Skip to content

Commit a6e5e5f

Browse files
committed
Clarify createAsyncThunk type handling
1 parent a38b3b7 commit a6e5e5f

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

docs/usage/usage-with-typescript.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -396,20 +396,21 @@ const wrappedSlice = createGenericSlice({
396396

397397
In the most common use cases, you should not need to explicitly declare any types for the `createAsyncThunk` call itself.
398398

399-
Just provide a type for the first argument to the `payloadCreator` argument as you would for any function argument, and the resulting thunk will accept the same type as input parameter.
399+
Just provide a type for the first argument to the `payloadCreator` argument as you would for any function argument, and the resulting thunk will accept the same type as its input parameter.
400400
The return type of the `payloadCreator` will also be reflected in all generated action types.
401401

402-
```ts {8,17}
403-
interface Returned {
402+
```ts {8,11,18}
403+
interface MyData {
404404
// ...
405405
}
406406

407407
const fetchUserById = createAsyncThunk(
408408
'users/fetchById',
409-
// if you type your function argument here
409+
// Declare the type your function argument here:
410410
async (userId: number) => {
411411
const response = await fetch(`https://reqres.in/api/users/${userId}`)
412-
return (await response.json()) as Returned
412+
// Inferred return type: Promise<MyData>
413+
return (await response.json()) as MyData
413414
}
414415
)
415416

@@ -419,13 +420,15 @@ const fetchUserById = createAsyncThunk(
419420
const lastReturnedAction = await store.dispatch(fetchUserById(3))
420421
```
421422

422-
The second argument to the `payloadCreator` is a `thunkApi` object containing references to the `dispatch`, `getState`, and `extra` arguments from the thunk middleware. If you want to use these from within the `payloadCreator`, you will need to define some generic arguments, as the types for these arguments cannot be inferred. Also, as TS cannot mix explicit and inferred generic parameters, from this point on you'll have to define the `Returned` and `ThunkArg` generic parameter as well.
423+
The second argument to the `payloadCreator`, known as `thunkApi`, is an object containing references to the `dispatch`, `getState`, and `extra` arguments from the thunk middleware. If you want to use these from within the `payloadCreator`, you will need to define some generic arguments, as the types for these arguments cannot be inferred. Also, as TS cannot mix explicit and inferred generic parameters, from this point on you'll have to define the `Returned` and `ThunkArg` generic parameter as well.
423424

424425
To define the types for these arguments, pass an object as the third generic argument, with type declarations for some or all of these fields: `{dispatch?, state?, extra?}`.
425426

426-
```ts {2-10}
427+
```ts {2-12}
427428
const fetchUserById = createAsyncThunk<
428-
Returned,
429+
// Return type of the payload creator
430+
Promise<MyData>,
431+
// First argument to the payload creator
429432
number,
430433
{
431434
dispatch: AppDispatch
@@ -440,7 +443,7 @@ const fetchUserById = createAsyncThunk<
440443
Authorization: `Bearer ${thunkApi.extra.jwt}`
441444
}
442445
})
443-
return await response.json()
446+
return (await response.json()) as MyData
444447
})
445448
```
446449

0 commit comments

Comments
 (0)