[3.0] Remove Action generic argument from configureStore#5345
Conversation
Review or Edit in CodeSandboxOpen the branch in Web Editor • VS Code • Insiders |
✅ Deploy Preview for redux-starter-kit-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Seems like the rejection would be correct? Reducers should not be typed to accept specific actions, that's the point of this change. Reducers can be called with any action (and will be), and should return state unchanged if the action is unrecognised. |
Good point - you're right, I'll switch it to UnknownAction. |
54f5803 to
be234c4
Compare
@reduxjs/rtk-codemods
@rtk-query/codegen-openapi
@rtk-query/graphql-request-base-query
@reduxjs/toolkit
commit: |
Closes #5317.
Drops the
Actiongeneric (A extends Action = UnknownAction) fromconfigureStore,ConfigureStoreOptionsandEnhancedStore.Anything other than
UnknownActionfor the store's action type has been discouraged for ages, but having the generic there kept inviting the opposite. People assume that if an action type can be passed in, it should flow throughdispatchand into middleware — then get surprised when it doesn't (reduxjs/redux#4887 is a good example). Removing the argument makes the contract obvious: the store always dispatchesUnknownAction, and you narrow with type guards where needed.What changed
The three types now have one fewer generic —
configureStore<S, M, E, P>andEnhancedStore<S, E>— and the store is alwaysStore<S, UnknownAction, …>.One thing worth calling out: the
reduceroption usesanyin the action slot (Reducer<S, any, P> | ReducersMapObject<S, any, P>) rather thanUnknownAction.UnknownActionthere rejects perfectly good reducers typed for a specific action — including anything fromcombineReducers, since that keeps a concrete action union — because of parameter contravariance understrictFunctionTypes.anykeeps accepting whatever reducer gets passed while the store type itself staysUnknownAction.The API docs are updated to match, and the
middleware/enhancersdescriptions are fixed too — they still claimed an array could be passed, which hasn't been true since 2.x (a maintainer pointed this out in redux#4887 as well).Breaking change
Aimed at 3.0. Nothing changes if the action type was never written out explicitly — the default was already
UnknownAction. It only bites something likeconfigureStore<State, MyAction>(…)or an explicitEnhancedStore<State, MyAction, …>annotation, since dropping the parameter shifts the remaining generics along. The fix is to remove the action type argument.Testing
Type checks and the
configureStoretypecheck suite pass, and the reworked type test confirms a specifically-typed reducer is still accepted and just produces anUnknownActionstore.