`configureStore()` middleware action typings are wrong
See original GitHub issueCurrently the middleware
option for configureStore()
does not affect what action types store.dispatch()
accepts; thunks are always accepted even if the thunk middleware is missing, and adding other middleware ignores its dispatch signature.
This is a regression from Redux, where the dispatchable action types are picked up correctly by createStore()
.
Example:
const store = configureStore({
reducer: rootReducer,
// default thunk middleware disabled
middleware: []
});
// expected compiler error missing
store.dispatch(() => {});
I’ve also made a longer working example that defines a number middleware that adds a number action type and shows that the types work with plain Redux but not RTK. I’ve tried to work around the issue by passing explicit type parameters like so: configureStore<RootState, AnyAction | number>
, but it doesn’t work due to always expecting a type property on the action.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Typing dispatch on a redux store correctly when using multiple ...
Adding on to this, I was able to add the redux-logger via return getDefaultMiddleware().concat(logger) in the middleware callback, but the ...
Read more >Usage With TypeScript - Redux
Type safety for reducers, state and action creators, and UI components ... Using configureStore should not need any additional typings.
Read more >How to get better and easier state management with Redux ...
Learn through a simple project what state management improvements Redux Toolkit has to offer and whether it will be ideal for your project....
Read more >Top 5 @reduxjs/toolkit Code Examples - Snyk
actions /action-types' import { createReducer } from '@reduxjs/toolkit' import ... import { configureStore, getDefaultMiddleware } from "@reduxjs/toolkit"; ...
Read more >Setting Up Redux Toolkit and Dispatching an Asynchronous ...
Dispatching an asynchronous action to the reducer ... “It returns an array containing the default middleware installed by ConfigureStore().
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
@slikts I just finished my work on the PR, so this will most likely make it into the next version of RTK. If you want to already use it, you can use the codesandbox build for the PR. Early feedback is highly welcome 😃
Fascinating, I always assumed that
redux
did not do that (as it is impossible in TS to type this for an arbitrary number of middlewares), but apparently the current types actually support the dispatch type of up to 5 middlewares.Thank you for bringing that to my attention. @markerikson I guess we should add support for this as well then, shouldn’t we?
In the meantime, the way to currently work around this is documented in the “Usage with TypeScript” documentation: Extending the Dispatch type