Default `EnhancedStore.dispatch` unusable in TS in 1.8.0?
See original GitHub issue1.8.0’s changes to middleware dispatch types apparently mean that the default EnhancedStore.dispatch
is typed as never
in TypeScript.
import { CombinedState, configureStore, EnhancedStore } from '@reduxjs/toolkit';
let store: EnhancedStore<CombinedState<{ name: string }>> | undefined;
store = configureStore({ reducer: (state) => state || { name: 'Test'} });
store.dispatch({});
In RTK 1.8.0, this results in the following TypeScript error:
This expression is not callable.
Type 'never' has no call signatures. ts(2349)
In RTK 1.7.2, it compiles without errors.
I can work around this by specifying my own middleware type argument to EnhancedStore
or by inferring the type with typeof
, but that feels much more awkward.
Thank you.
Issue Analytics
- State:
- Created 2 years ago
- Comments:19 (3 by maintainers)
Top Results From Across the Web
this expression is not callable. type 'never' has no call ...
1.8.0's changes to middleware dispatch types apparently mean that the default EnhancedStore.dispatch is typed as never in TypeScript.
Read more >ReactJS Redux Provider won't take the store - Stack Overflow
The default middleware is, as the name implies, the default. Also, please note that the deprecation of createStor is just an indicator that ......
Read more >Usage With TypeScript - Redux Toolkit
By default, the React Redux useDispatch hook does not contain any types that take middlewares into account. If you need a more specific...
Read more >Using TypeScript with Redux Toolkit - LogRocket Blog
Configuring the store with configureStore · // src/app/rootReducer.ts · import · from · '@reduxjs/toolkit' · const · export · export · default ......
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
Okay, the issue here is the use of a separate
ConfigureStoreOptions
. You’ve erased any actual types that could be inferred.Please don’t write that separately. If you do everything inline in
configureStore
, the types are correct:It looks like the main reason you’re doing that is to make the
devTools
option configurable. I’d suggest just having a separatelet devTools
boolean and conditionally assign to that, then pass it as an argument toconfigureStore
.Generally, when using RTK you should never declare an explicit type for a variable. That is erasing all type information until there is only the info “Yeah, this has the shape of a configuration. Maybe there are reducers in there. Maybe middleware.” is left.