Describe inferring RootState type from store.getState
See original GitHub issueThe docs show inferring type RootState = ReturnType<typeof rootReducer>
. But, if you pass the slice reducers directly to configureStore()
, you never created the root reducer yourself, so you need to infer it as type RootState = ReturnType<typeof store.getState>
instead.
We should update the Advanced Tutorial and Usage with TypeScript pages to mention that.
See https://stackoverflow.com/a/59827580/62937 for an example of this
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:29 (14 by maintainers)
Top Results From Across the Web
Usage With TypeScript - Redux Toolkit
The easiest way of getting the State type is to define the root reducer in advance and extract its ReturnType . It is...
Read more >Typing Redux toolkit's store in TypeScript - Stack Overflow
There's really three ways to define that: 1) hand-define it yourself; 2) Infer it from the return type of the root reducer; 3)...
Read more >How I Setup Redux Toolkit and RTK Query the right way
Define the Root State and Dispatch Types ... We need to extract the RootState and AppDispatch from the store and export them directly...
Read more >How to Use Redux Toolkit with TypeScript - Fullstack.io
Define the `RootState` as the return type: export type RootState = ReturnType<typeof store.getState>;. Now return to todosSlice.ts and ...
Read more >Static Typing | React Redux
While this type could be written by hand, the easiest way to define it is to have TypeScript infer it based on what...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@msutkowski Thank you very much! You saved me quite a few hours of debugging 😉 It seems that in my case it was a problem with the reducer builder. For example instead of
builder => builder.addCase(...)
, I should havebuilder => { builder.addCase(...) }
.@markerikson Why in this case these brackets are so important? Is the typing system somehow relies on the return value form the builder?
Such changes would conflict with this advice from docs on
getDefaultMiddleware<RootState>
https://redux-starter-kit.js.org/usage/usage-with-typescript#correct-typings-for-the-dispatch-type