new·Earn with mozg — 20% of every monthSend somebody here and take a fifth of every plan payment they make, for as long as they keep paying — not a bounty on the first invoice. Your handle is the link, the window is thirty days, and the commission lands on your balance the second they pay. Free to join: if you have signed in, you already have the link. mozg.sh/earnall news →
mozg.beta
Sign in

Redux Toolkit · API · all subjects

type safety patterns

6 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Export AppDispatch and RootState types for type safety

Export type RootState = ReturnType<typeof store.getState> and export type AppDispatch = typeof store.dispatch from your store setup file. These types ensure consistent typing across your application.

TypeScript pre-typing listener middleware

To properly type startListening and addListener with the store's RootState, create pre-typed versions using withTypes<RootState, AppDispatch, ExtraArgument>(). Create the middleware instance in a separate file from configureStore, then export startAppListening and addAppListener as pre-typed methods for use in components.

Redux Toolkit TypeScript support version requirements

Redux Toolkit follows DefinitelyTyped's policy of supporting TypeScript versions released within the past two years. As of RTK 2.11: RTK 2.x requires minimum TypeScript 5.4+, and RTK 1.9.x requires minimum TypeScript 4.7+.

AnyAction deprecated in favor of UnknownAction

In Redux 5.0, the AnyAction type is deprecated in favor of UnknownAction. UnknownAction treats all fields other than action.type as unknown, encouraging type guards to check the action object and assert its specific type. AnyAction still exists for compatibility but should not be used in new code.

PreloadedState generic removed from Reducer type

In Redux 5.0, the separate PreloadedState type was removed and instead integrated as a third generic on the Reducer type itself: type Reducer<S, A extends Action, PreloadedState = S>. This allows createStore and combineReducers to better type preloadedState arguments.

combineReducers overloads removed

In Redux 5.0, the combineReducers function no longer uses multiple overloads. Instead it takes the ReducersMapObject as a single generic parameter and has a single function definition. Enhancers that explicitly list the generics for the reducer now need to add the third PreloadedState generic.

Give your agent this brain