reduxjs / redux-toolkit

The official, opinionated, batteries-included toolset for efficient Redux development
https://redux-toolkit.js.org
MIT License
10.71k stars 1.17k forks source link

type mismatch on `middleware` property of `configureStore` when also using `enhancers` #4248

Closed burtek closed 7 months ago

burtek commented 8 months ago
import { configureStore } from '@reduxjs/toolkit';
import { createLogger } from 'redux-logger';

export const store = configureStore({
    enhancers: getDefaultEnhancers => getDefaultEnhancers(),
    middleware: getDefaultMiddleware => {
        return getDefaultMiddleware().prepend(createLogger())
    },
    reducer: {}
});

reports TS 2322 error on middleware key:

Type '(getDefaultMiddleware: GetDefaultMiddleware<any>) => Tuple<[Middleware<{}, any, Dispatch<UnknownAction>>, ThunkMiddleware<any, UnknownAction>]>' is not assignable to type '(getDefaultMiddleware: GetDefaultMiddleware<any>) => Tuple<[ThunkMiddleware<any, UnknownAction>]>'.
  Type 'Tuple<[Middleware<{}, any, Dispatch<UnknownAction>>, ThunkMiddleware<any, UnknownAction>]>' is not assignable to type 'Tuple<[ThunkMiddleware<any, UnknownAction>]>'.
    Type '[Middleware<{}, any, Dispatch<UnknownAction>>, ThunkMiddleware<any, UnknownAction>]' is not assignable to type '[ThunkMiddleware<any, UnknownAction>]'.
      Source has 2 element(s) but target allows only 1.

image


This does not happen if the enhancers key is not provided:

image

NB: It's not the case of "remove enhancers key since you're not doing anything there" - in my project I do add more enhancers. Here I provided minimal repro code.


Using:

lib version
@reduxjs/toolkit 2.2.1
redux-logger 3.0.6
typescript 5.3.2
EskiMojo14 commented 8 months ago

the type of getDefaultEnhancers depends on the result of your middleware callback, move the enhancers key after the middleware key.

i thought we had documented this somewhere but I can't seem to find it

burtek commented 8 months ago

How the heck do types depend on keys order o.O

Anyway, it worked. Thanks. Would be nice to document that properly :)

markerikson commented 8 months ago

We mentioned it in the migration guide:

but not sure it's in the actual docs.

burtek commented 8 months ago

Well, I wasn't migrating, so I never found that part :/ Good to hear it's in at least that place, but I'd expect it somewhere here too: https://redux-toolkit.js.org/api/configureStore#middleware / https://redux-toolkit.js.org/api/configureStore#enhancers

Happy to close this issue unless you want to use it to track any related docs changes