Skip to content

Commit b865fb1

Browse files
author
ben.durrant
committed
consistency
1 parent 748a32b commit b865fb1

File tree

1 file changed

+8
-24
lines changed

1 file changed

+8
-24
lines changed

packages/toolkit/src/configureStore.ts

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,13 @@ export interface ConfigureStoreOptions<
4444
A extends Action = AnyAction,
4545
M extends Middlewares<S> = Middlewares<S>,
4646
E extends Enhancers = Enhancers,
47-
PreloadedState = S
47+
P = S
4848
> {
4949
/**
5050
* A single reducer function that will be used as the root reducer, or an
5151
* object of slice reducers that will be passed to `combineReducers()`.
5252
*/
53-
reducer:
54-
| Reducer<S, A, PreloadedState>
55-
| ReducersMapObject<S, A, PreloadedState>
53+
reducer: Reducer<S, A, P> | ReducersMapObject<S, A, P>
5654

5755
/**
5856
* An array of Redux middleware to install. If not supplied, defaults to
@@ -78,16 +76,8 @@ export interface ConfigureStoreOptions<
7876
* function (either directly or indirectly by passing an object as `reducer`),
7977
* this must be an object with the same shape as the reducer map keys.
8078
*/
81-
/*
82-
Not 100% correct but the best approximation we can get:
83-
- if S is a `CombinedState` applying a second `CombinedState` on it does not change anything.
84-
- if it is not, there could be two cases:
85-
- `ReducersMapObject<S, A>` is being passed in. In this case, we will call `combineReducers` on it and `CombinedState<S>` is correct
86-
- `Reducer<S, A>` is being passed in. In this case, actually `CombinedState<S>` is wrong and `S` would be correct.
87-
As we cannot distinguish between those two cases without adding another generic parameter,
88-
we just make the pragmatic assumption that the latter almost never happens.
89-
*/
90-
preloadedState?: PreloadedState
79+
// we infer here, and instead complain if the reducer doesn't match
80+
preloadedState?: P
9181

9282
/**
9383
* The store enhancers to apply. See Redux's `createStore()`.
@@ -143,10 +133,8 @@ export function configureStore<
143133
A extends Action = AnyAction,
144134
M extends Middlewares<S> = [ThunkMiddlewareFor<S>],
145135
E extends Enhancers = [StoreEnhancer],
146-
PreloadedState = S
147-
>(
148-
options: ConfigureStoreOptions<S, A, M, E, PreloadedState>
149-
): EnhancedStore<S, A, M, E> {
136+
P = S
137+
>(options: ConfigureStoreOptions<S, A, M, E, P>): EnhancedStore<S, A, M, E> {
150138
const curriedGetDefaultMiddleware = curryGetDefaultMiddleware<S>()
151139

152140
const {
@@ -157,16 +145,12 @@ export function configureStore<
157145
enhancers = undefined,
158146
} = options || {}
159147

160-
let rootReducer: Reducer<S, A, PreloadedState>
148+
let rootReducer: Reducer<S, A, P>
161149

162150
if (typeof reducer === 'function') {
163151
rootReducer = reducer
164152
} else if (isPlainObject(reducer)) {
165-
rootReducer = combineReducers(reducer) as unknown as Reducer<
166-
S,
167-
A,
168-
PreloadedState
169-
>
153+
rootReducer = combineReducers(reducer) as unknown as Reducer<S, A, P>
170154
} else {
171155
throw new Error(
172156
'"reducer" is a required argument, and must be a function or an object of functions that can be passed to combineReducers'

0 commit comments

Comments
 (0)