Skip to content

Commit 4b9977e

Browse files
authored
Merge pull request #4225 from k-yle/satisfies
suggest using the `satisfies` operator for `initialState`
2 parents 409a16d + d99d955 commit 4b9977e

File tree

10 files changed

+21
-21
lines changed

10 files changed

+21
-21
lines changed

docs/api/autoBatchEnhancer.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ interface CounterState {
2727

2828
const counterSlice = createSlice({
2929
name: 'counter',
30-
initialState: { value: 0 } as CounterState,
30+
initialState: { value: 0 } satisfies CounterState as CounterState,
3131
reducers: {
3232
incrementBatched: {
3333
// Batched, low-priority

docs/api/createAsyncThunk.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ const fetchUserById = createAsyncThunk(
3939
)
4040

4141
interface UsersState {
42-
entities: []
42+
entities: User[]
4343
loading: 'idle' | 'pending' | 'succeeded' | 'failed'
4444
}
4545

4646
const initialState = {
4747
entities: [],
4848
loading: 'idle',
49-
} as UsersState
49+
} satisfies UserState as UsersState
5050

5151
// Then, handle actions in your reducers:
5252
const usersSlice = createSlice({
@@ -699,7 +699,7 @@ interface UsersState {
699699
const initialState = {
700700
entities: {},
701701
error: null,
702-
} as UsersState
702+
} satisfies UsersState as UsersState
703703

704704
const usersSlice = createSlice({
705705
name: 'users',

docs/api/createReducer.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const increment = createAction('counter/increment')
5252
const decrement = createAction('counter/decrement')
5353
const incrementByAmount = createAction<number>('counter/incrementByAmount')
5454

55-
const initialState = { value: 0 } as CounterState
55+
const initialState = { value: 0 } satisfies CounterState as CounterState
5656

5757
const counterReducer = createReducer(initialState, (builder) => {
5858
builder

docs/api/createSlice.mdx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ interface CounterState {
2525
value: number
2626
}
2727

28-
const initialState = { value: 0 } as CounterState
28+
const initialState = { value: 0 } satisfies CounterState as CounterState
2929

3030
const counterSlice = createSlice({
3131
name: 'counter',
@@ -158,7 +158,7 @@ const todosSlice = createSlice({
158158
initialState: {
159159
loading: false,
160160
todos: [],
161-
} as TodoState,
161+
} satisfies TodoState as TodoState,
162162
reducers: (create) => ({
163163
deleteTodo: create.reducer<number>((state, action) => {
164164
state.todos.splice(action.payload, 1)
@@ -513,7 +513,7 @@ interface CounterState {
513513

514514
const counterSlice = createSlice({
515515
name: 'counter',
516-
initialState: { value: 0 } as CounterState,
516+
initialState: { value: 0 } satisfies CounterState as CounterState,
517517
reducers: {
518518
// omitted
519519
},
@@ -542,7 +542,7 @@ interface CounterState {
542542

543543
const counterSlice = createSlice({
544544
name: 'counter',
545-
initialState: { value: 0 } as CounterState,
545+
initialState: { value: 0 } satisfies CounterState as CounterState,
546546
reducers: {
547547
// omitted
548548
},
@@ -608,7 +608,7 @@ const decrementBy = createAction<number>('decrementBy')
608608

609609
const counter = createSlice({
610610
name: 'counter',
611-
initialState: 0 as number,
611+
initialState: 0 satisfies number as number,
612612
reducers: {
613613
increment: (state) => state + 1,
614614
decrement: (state) => state - 1,

docs/api/matching-utilities.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ interface ExampleState {
181181
const initialState = {
182182
isSpecial: false,
183183
isInteresting: false,
184-
} as ExampleState
184+
} satisfies ExampleState as ExampleState
185185

186186
export const isSpecialAndInterestingThunk = createAsyncThunk(
187187
'isSpecialAndInterestingThunk',

docs/api/otherExports.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ interface Todo {
6666
}
6767
const addTodo = createAction<Todo>('addTodo')
6868

69-
const initialState = [] as Todo[]
69+
const initialState = [] satisfies Todo[] as Todo[]
7070

7171
const todosReducer = createReducer(initialState, (builder) => {
7272
builder.addCase(addTodo, (state, action) => {

docs/tutorials/typescript.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ In some cases, [TypeScript may unnecessarily tighten the type of the initial sta
149149
// Workaround: cast state instead of declaring variable type
150150
const initialState = {
151151
value: 0,
152-
} as CounterState
152+
} satisfies CounterState as CounterState
153153
```
154154

155155
### Use Typed Hooks in Components

docs/usage/usage-with-typescript.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ createSlice({
273273

274274
You might have noticed that it is not a good idea to pass your `SliceState` type as a generic to `createSlice`. This is due to the fact that in almost all cases, follow-up generic parameters to `createSlice` need to be inferred, and TypeScript cannot mix explicit declaration and inference of generic types within the same "generic block".
275275

276-
The standard approach is to declare an interface or type for your state, create an initial state value that uses that type, and pass the initial state value to `createSlice`. You can also use the construct `initialState: myInitialState as SliceState`.
276+
The standard approach is to declare an interface or type for your state, create an initial state value that uses that type, and pass the initial state value to `createSlice`. You can also use the construct `initialState: myInitialState satisfies SliceState as SliceState`.
277277

278278
```ts {1,4,8,15}
279279
type SliceState = { state: 'loading' } | { state: 'finished'; data: string }
@@ -290,7 +290,7 @@ createSlice({
290290
// Or, cast the initial state as necessary
291291
createSlice({
292292
name: 'test2',
293-
initialState: { state: 'loading' } as SliceState,
293+
initialState: { state: 'loading' } satisfies SliceState as SliceState,
294294
reducers: {},
295295
})
296296
```
@@ -368,14 +368,14 @@ const fetchUserById = createAsyncThunk(
368368
)
369369

370370
interface UsersState {
371-
entities: []
371+
entities: User[]
372372
loading: 'idle' | 'pending' | 'succeeded' | 'failed'
373373
}
374374

375375
const initialState = {
376376
entities: [],
377377
loading: 'idle',
378-
} as UsersState
378+
} satisfies UsersState as UsersState
379379

380380
const usersSlice = createSlice({
381381
name: 'users',

docs/virtual/matchers/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export interface ExampleState {
3737
export const initialState = {
3838
isSpecial: false,
3939
isInteresting: false,
40-
} as ExampleState
40+
} satisfies ExampleState as ExampleState
4141

4242
export const isSpecialAndInterestingThunk = createAsyncThunk(
4343
'isSpecialAndInterestingThunk',

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23637,11 +23637,11 @@ __metadata:
2363723637
linkType: hard
2363823638

2363923639
"prettier@npm:^2.1.1":
23640-
version: 2.3.1
23641-
resolution: "prettier@npm:2.3.1"
23640+
version: 2.8.8
23641+
resolution: "prettier@npm:2.8.8"
2364223642
bin:
2364323643
prettier: bin-prettier.js
23644-
checksum: 10/dae97c14da795b789f89c84f83f86003a10482b572d1fc365b54fea81277721795da1a6a44599a655d1131ff2ea8467bae0c5ecd9def15dca67b7fe740fe2e0e
23644+
checksum: 10/00cdb6ab0281f98306cd1847425c24cbaaa48a5ff03633945ab4c701901b8e96ad558eb0777364ffc312f437af9b5a07d0f45346266e8245beaf6247b9c62b24
2364523645
languageName: node
2364623646
linkType: hard
2364723647

0 commit comments

Comments
 (0)