Unable to place logger and thunk in configurestore config middleware property.
See original GitHub issueI am encoutering a typescript error when adding both redux logger and redux thunk to my store.
store/index.ts
import { combineReducers, configureStore } from '@reduxjs/toolkit';
import reduxLogger from 'redux-logger';
import reduxThunk from 'redux-thunk';
import authReducer from './auth/reducers';
import menusReducer from './menus/reducers';
import monitorReducerEnhancer from './enhancers/monitorReducers';
const rootReducer = combineReducers({
auth: authReducer,
menus: menusReducer,
});
const store = configureStore({
reducer: rootReducer,
middleware: [reduxLogger, reduxThunk],
enhancers: [monitorReducerEnhancer],
});
export type AppDispatch = typeof store.dispatch;
export type RootState = ReturnType<typeof rootReducer>;
export default store;
and the error:
TypeScript error in /store/index.ts(17,5):
Property '0' is missing in type '(Middleware | (ThunkMiddleware<{}, AnyAction, undefined> & { withExtraArgument<E>(extraArgument: E): ThunkMiddleware<{}, AnyAction, E>; }))[]' but required in type '[ThunkMiddleware<CombinedState<{ auth: { fetched: boolean; fetching: boolean; error: {}; data: {}; }; menus: {}; }>, AnyAction, null> | ThunkMiddleware<...>]'. TS2741
15 | const store = configureStore({
16 | reducer: rootReducer,
> 17 | middleware: [reduxLogger, reduxThunk],
| ^
18 | enhancers: [monitorReducerEnhancer],
19 | });
20 |
I believe the issue is caused by this line: https://github.com/reduxjs/redux-toolkit/blob/master/src/configureStore.ts#L125
M extends Middlewares<S> = [ThunkMiddlewareFor<S>]
The error goes away when changing to
M extends Middlewares<S> = ThunkMiddlewareFor<S>[]
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
configureStore - Redux Toolkit
configureStore accepts a single configuration object parameter, with the following options: type ConfigureEnhancersCallback = (
Read more >having error with getdefaultmiddleware of redux toolkit in ...
The somewhat shorter option would be: export const store = configureStore({ reducer: persistedReducer, middleware: (getDefaultMiddleware) ...
Read more >How to Use Thunks with Redux Toolkit and TypeScript
All the work is done in the redux-thunk middleware. It extends the store's abilities and lets you write async logic that interacts with...
Read more >Redux Toolkit Tutorial - 22 - Logger Middleware - YouTube
Your browser can't play this video. Learn more. Switch camera ... Redux Toolkit Tutorial - 22 - Logger Middleware. 13K views 7 months...
Read more >redux-thunk - npm
Thunk middleware for Redux.. Latest version: 2.4.2, last published: 2 months ago. Start using redux-thunk in your project by running `npm i ...
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
It seems to struggle because the default-export from
redux-thunk
is not only aThunkMiddleware
, but of typeThunkMiddleware & { withExtraArgument<E>(extraArgument: E): ThunkMiddleware<{}, AnyAction, E> }
.Just casting reduxThunk in the above example does the trick:
I’ll have investigate this further when I have the time and weirdly, a similar problem appears to happen with getDefaultMiddleware, but for now I hope that this is a fix on your side you can live with.
@chanced probably #552 - #559 will fix is, but isn’t reviewed & merged yet. Until that, #552 contains a workaround.