Skip to content

Commit fb5dd97

Browse files
committed
docs: warn against custom middleware + typeof store.getState TS circular reference
1 parent 82f0f90 commit fb5dd97

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

docs/usage/usage-with-typescript.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,23 @@ const store = configureStore({
3939
export type RootState = ReturnType<typeof store.getState>
4040
```
4141
42+
Note that passing the reducers directly is not recommended when you need to add custom middleware,
43+
because this can cause a circular reference in TypeScript:
44+
45+
```typescript
46+
import { configureStore } from '@reduxjs/toolkit'
47+
import rootReducer from './rootReducer'
48+
const store = configureStore({
49+
reducer: rootReducer,
50+
middleware: [
51+
...customMiddleware,
52+
...getDefaultMiddleware<RootState>(),
53+
] as const,
54+
})
55+
// TS2456: Type alias 'RootState' circularly references itself.
56+
export type RootState = ReturnType<typeof store.getState>
57+
```
58+
4259
### Getting the `Dispatch` type
4360
4461
If you want to get the `Dispatch` type from your store, you can extract it after creating the store.

0 commit comments

Comments
 (0)