question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to place logger and thunk in configurestore config middleware property.

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
phryneascommented, Feb 14, 2020

It seems to struggle because the default-export from redux-thunk is not only a ThunkMiddleware, but of type ThunkMiddleware & { withExtraArgument<E>(extraArgument: E): ThunkMiddleware<{}, AnyAction, E> }.

Just casting reduxThunk in the above example does the trick:


const store = configureStore({
    reducer: rootReducer,
-    middleware: [reduxLogger, reduxThunk],
+    middleware: [reduxLogger, reduxThunk as ThunkMiddleware],
    enhancers: [monitorReducerEnhancer],
});

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.

0reactions
phryneascommented, May 17, 2020

@chanced probably #552 - #559 will fix is, but isn’t reviewed & merged yet. Until that, #552 contains a workaround.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found