Skip to content

Commit daf9920

Browse files
committed
Add example of createAsyncThunk.withTypes
1 parent 74e7cb4 commit daf9920

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

docs/usage/usage-with-typescript.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,18 @@ export type RootState = ReturnType<typeof store.getState>
6262
export default store
6363
```
6464

65-
If you pass the reducers directly to `configureStore()` and do not define the root reducer explicitly, there is no reference to `rootReducer`.
65+
If you pass the reducers directly to `configureStore()` and do not define the root reducer explicitly, there is no reference to `rootReducer`.
6666
Instead, you can refer to `store.getState`, in order to get the `State` type.
6767

6868
```typescript
6969
import { configureStore } from '@reduxjs/toolkit'
7070
import rootReducer from './rootReducer'
7171
const store = configureStore({
72-
reducer: rootReducer
72+
reducer: rootReducer,
7373
})
7474
export type RootState = ReturnType<typeof store.getState>
7575
```
7676
77-
7877
### Getting the `Dispatch` type
7978
8079
If you want to get the `Dispatch` type from your store, you can extract it after creating the store. It is recommended to give the type a different name like `AppDispatch` to prevent confusion, as the type name `Dispatch` is usually overused. You may also find it to be more convenient to export a hook like `useAppDispatch` shown below, then using it wherever you'd call `useDispatch`.
@@ -489,6 +488,8 @@ const wrappedSlice = createGenericSlice({
489488

490489
## `createAsyncThunk`
491490

491+
### Basic `createAsyncThunk` Types
492+
492493
In the most common use cases, you should not need to explicitly declare any types for the `createAsyncThunk` call itself.
493494

494495
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.
@@ -518,8 +519,12 @@ const fetchUserById = createAsyncThunk(
518519
const lastReturnedAction = await store.dispatch(fetchUserById(3))
519520
```
520521

522+
### Typing the `thunkApi` Object
523+
521524
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 as well as a utility function called `rejectWithValue`. 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.
522525

526+
#### Manually Defining `thunkApi` Types
527+
523528
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:
524529

525530
```ts
@@ -662,6 +667,23 @@ const handleUpdateUser = async (userData) => {
662667
}
663668
```
664669

670+
#### Defining a Pre-Typed `createAsyncThunk`
671+
672+
As of RTK 1.9, you can define a "pre-typed" version of `createAsyncThunk` that can have the types for `state`, `dispatch`, and `extra` built in. This lets you set up those types once, so you don't have to repeat them each time you call `createAsyncThunk`.
673+
674+
To do this, call `createAsyncThunk.withTypes<>()`, and pass in an object containing the field names and types for any of the fields in the `AsyncThunkConfig` type listed above. This might look like:
675+
676+
```ts
677+
const createAppAsyncThunk = createAsyncThunk.withTypes<{
678+
state: RootState
679+
dispatch: AppDispatch
680+
rejectValue: string
681+
extra: { s: string; n: number }
682+
}>()
683+
```
684+
685+
Import and use that pre-typed `createAppAsyncThunk` instead of the original, and the types will be used automatically:
686+
665687
## `createEntityAdapter`
666688

667689
Typing `createEntityAdapter` only requires you to specify the entity type as the single generic argument.

0 commit comments

Comments
 (0)