-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
What to do to replace removed PreloadedState
type in v2?
#3946
Comments
I found replacing it with |
the complete answer is if you've used One thing to note is that it looks like you're using Redux Persist, which hasn't been updated to handle v5's PreloadedState generic (and honestly may never - it looks like it's rarely maintained). You can fix this yourself, by adding an overload via module augmentation: import type { Action, Reducer } from "redux";
import type { PersistConfig, PersistState } from "redux-persist";
declare module "redux-persist" {
export function persistReducer<S, A extends Action = Action, P = S>(
config: PersistConfig<S>,
baseReducer: Reducer<S, A, P>,
): Reducer<
S & { _persist: PersistState },
A,
P & { _persist?: PersistState }
>;
} Here's a playground so you can see the difference. |
Also having the same problem, using only |
You can also use a type helper to extract the preloaded state from a given reducer: import type { Reducer } from "redux";
type PreloadedStateFromReducer<R extends Reducer<any, any, any>> = R extends Reducer<any, any, infer P> ? P : never
type PreloadedState = PreloadedStateFromReducer<typeof reducer> Generally though it's entirely valid to just do |
In https://redux.js.org/usage/migrations/migrating-rtk-2#preloadedstate-type-removed-in-favour-of-reducer-generic authors write type Reducer<S, A extends Action, PreloadedState = S> = (
state: S | PreloadedState | undefined,
action: A
) => S <...>
I understood it in such a way:
@markerikson do you agree with it? |
essentially, yes - if your root reducer was created with in the rare case of nested const rootReducer = combineReducers({ foo, bar: combineReducers({ baz, qux }) });
type PreloadedState = Partial<{ foo: FooState; bar: Partial<{ baz: BazState; qux: QuxState }> }> The type helper above would assist with getting the correct type. |
Usecase: I have a
createStore
utility function that takes thepreloadedState
as parameter, and pass it toconfigureStore
, for generating the desired redux store state in testing.As you can see in the code snippet below, what should I use now to replace
PreloadState<RootState>
?Based on the migration guide, it's using generic to infer the type now, but I don't see how it can help with my use case
The text was updated successfully, but these errors were encountered: