From 501f579631fe3444d274451da9991fe71ba1c9c2 Mon Sep 17 00:00:00 2001 From: Lenz Weber Date: Sun, 28 Mar 2021 12:40:31 +0200 Subject: [PATCH 1/2] fix PreloadedState type for TS 4.3 --- src/configureStore.ts | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/configureStore.ts b/src/configureStore.ts index b74e24d54e..f4a690ee88 100644 --- a/src/configureStore.ts +++ b/src/configureStore.ts @@ -10,8 +10,8 @@ import { AnyAction, StoreEnhancer, Store, - DeepPartial, - Dispatch + Dispatch, + PreloadedState } from 'redux' import { composeWithDevTools, @@ -24,7 +24,7 @@ import { curryGetDefaultMiddleware, CurriedGetDefaultMiddleware } from './getDefaultMiddleware' -import { DispatchForMiddlewares } from './tsHelpers' +import { DispatchForMiddlewares, NoInfer } from './tsHelpers' const IS_PRODUCTION = process.env.NODE_ENV === 'production' @@ -74,11 +74,7 @@ export interface ConfigureStoreOptions< * function (either directly or indirectly by passing an object as `reducer`), * this must be an object with the same shape as the reducer map keys. */ - // NOTE: The needlessly complicated `S extends any ? S : S` instead of just - // `S` ensures that the TypeScript compiler doesn't attempt to infer `S` - // based on the value passed as `preloadedState`, which might be a partial - // state rather than the full thing. - preloadedState?: DeepPartial + preloadedState?: PreloadedState> /** * The store enhancers to apply. See Redux's `createStore()`. @@ -173,5 +169,5 @@ export function configureStore< const composedEnhancer = finalCompose(...storeEnhancers) as any - return createStore(rootReducer, preloadedState as any, composedEnhancer) + return createStore(rootReducer, preloadedState, composedEnhancer) } From e7d51621873d61ab6633bd600b0fab46cf9eb61f Mon Sep 17 00:00:00 2001 From: Lenz Weber Date: Sun, 28 Mar 2021 14:36:43 +0200 Subject: [PATCH 2/2] change type --- etc/redux-toolkit.api.md | 5 +++-- src/configureStore.ts | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/etc/redux-toolkit.api.md b/etc/redux-toolkit.api.md index 90d4314de8..96c217c635 100644 --- a/etc/redux-toolkit.api.md +++ b/etc/redux-toolkit.api.md @@ -7,10 +7,10 @@ import { Action } from 'redux'; import { ActionCreator } from 'redux'; import { AnyAction } from 'redux'; +import { CombinedState } from 'redux'; import { default as createNextState } from 'immer'; import { createSelector } from 'reselect'; import { current } from 'immer'; -import { DeepPartial } from 'redux'; import { Dispatch } from 'redux'; import { Draft } from 'immer'; import { freeze } from 'immer'; @@ -20,6 +20,7 @@ import { original } from 'immer'; import { OutputParametricSelector } from 'reselect'; import { OutputSelector } from 'reselect'; import { ParametricSelector } from 'reselect'; +import { PreloadedState } from 'redux'; import { Reducer } from 'redux'; import { ReducersMapObject } from 'redux'; import { Selector } from 'reselect'; @@ -127,7 +128,7 @@ export interface ConfigureStoreOptions) => M) | M; - preloadedState?: DeepPartial; + preloadedState?: PreloadedState>>; reducer: Reducer | ReducersMapObject; } diff --git a/src/configureStore.ts b/src/configureStore.ts index f4a690ee88..007b59887f 100644 --- a/src/configureStore.ts +++ b/src/configureStore.ts @@ -11,7 +11,8 @@ import { StoreEnhancer, Store, Dispatch, - PreloadedState + PreloadedState, + CombinedState } from 'redux' import { composeWithDevTools, @@ -74,7 +75,16 @@ export interface ConfigureStoreOptions< * function (either directly or indirectly by passing an object as `reducer`), * this must be an object with the same shape as the reducer map keys. */ - preloadedState?: PreloadedState> + /* + Not 100% correct but the best approximation we can get: + - if S is a `CombinedState` applying a second `CombinedState` on it does not change anything. + - if it is not, there could be two cases: + - `ReducersMapObject` is being passed in. In this case, we will call `combineReducers` on it and `CombinedState` is correct + - `Reducer` is being passed in. In this case, actually `CombinedState` is wrong and `S` would be correct. + As we cannot distinguish between those two cases without adding another generic paramter, + we just make the pragmatic assumption that the latter almost never happens. + */ + preloadedState?: PreloadedState>> /** * The store enhancers to apply. See Redux's `createStore()`.