@@ -10,8 +10,9 @@ import {
10
10
AnyAction ,
11
11
StoreEnhancer ,
12
12
Store ,
13
- DeepPartial ,
14
- Dispatch
13
+ Dispatch ,
14
+ PreloadedState ,
15
+ CombinedState
15
16
} from 'redux'
16
17
import {
17
18
composeWithDevTools ,
@@ -24,7 +25,7 @@ import {
24
25
curryGetDefaultMiddleware ,
25
26
CurriedGetDefaultMiddleware
26
27
} from './getDefaultMiddleware'
27
- import { DispatchForMiddlewares } from './tsHelpers'
28
+ import { DispatchForMiddlewares , NoInfer } from './tsHelpers'
28
29
29
30
const IS_PRODUCTION = process . env . NODE_ENV === 'production'
30
31
@@ -74,11 +75,16 @@ export interface ConfigureStoreOptions<
74
75
* function (either directly or indirectly by passing an object as `reducer`),
75
76
* this must be an object with the same shape as the reducer map keys.
76
77
*/
77
- // NOTE: The needlessly complicated `S extends any ? S : S` instead of just
78
- // `S` ensures that the TypeScript compiler doesn't attempt to infer `S`
79
- // based on the value passed as `preloadedState`, which might be a partial
80
- // state rather than the full thing.
81
- preloadedState ?: DeepPartial < S extends any ? S : S >
78
+ /*
79
+ Not 100% correct but the best approximation we can get:
80
+ - if S is a `CombinedState` applying a second `CombinedState` on it does not change anything.
81
+ - if it is not, there could be two cases:
82
+ - `ReducersMapObject<S, A>` is being passed in. In this case, we will call `combineReducers` on it and `CombinedState<S>` is correct
83
+ - `Reducer<S, A>` is being passed in. In this case, actually `CombinedState<S>` is wrong and `S` would be correct.
84
+ As we cannot distinguish between those two cases without adding another generic paramter,
85
+ we just make the pragmatic assumption that the latter almost never happens.
86
+ */
87
+ preloadedState ?: PreloadedState < CombinedState < NoInfer < S > > >
82
88
83
89
/**
84
90
* The store enhancers to apply. See Redux's `createStore()`.
@@ -173,5 +179,5 @@ export function configureStore<
173
179
174
180
const composedEnhancer = finalCompose ( ...storeEnhancers ) as any
175
181
176
- return createStore ( rootReducer , preloadedState as any , composedEnhancer )
182
+ return createStore ( rootReducer , preloadedState , composedEnhancer )
177
183
}
0 commit comments