Skip to content

Commit 8e2928c

Browse files
committed
Update types to work with the PreloadedState generic
1 parent 65902f5 commit 8e2928c

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

packages/toolkit/src/configureStore.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import type {
77
StoreEnhancer,
88
Store,
99
Dispatch,
10-
PreloadedState,
11-
CombinedState,
1210
} from 'redux'
1311
import { createStore, compose, applyMiddleware, combineReducers } from 'redux'
1412
import type { DevToolsEnhancerOptions as DevToolsOptions } from './devtoolsExtension'
@@ -21,7 +19,6 @@ import type {
2119
} from './getDefaultMiddleware'
2220
import { curryGetDefaultMiddleware } from './getDefaultMiddleware'
2321
import type {
24-
NoInfer,
2522
ExtractDispatchExtensions,
2623
ExtractStoreExtensions,
2724
} from './tsHelpers'
@@ -45,14 +42,17 @@ export type ConfigureEnhancersCallback<E extends Enhancers = Enhancers> = (
4542
export interface ConfigureStoreOptions<
4643
S = any,
4744
A extends Action = AnyAction,
45+
PreloadedState = S,
4846
M extends Middlewares<S> = Middlewares<S>,
4947
E extends Enhancers = Enhancers
5048
> {
5149
/**
5250
* A single reducer function that will be used as the root reducer, or an
5351
* object of slice reducers that will be passed to `combineReducers()`.
5452
*/
55-
reducer: Reducer<S, A> | ReducersMapObject<S, A>
53+
reducer:
54+
| Reducer<S, A, PreloadedState>
55+
| ReducersMapObject<S, A, PreloadedState>
5656

5757
/**
5858
* An array of Redux middleware to install. If not supplied, defaults to
@@ -87,7 +87,7 @@ export interface ConfigureStoreOptions<
8787
As we cannot distinguish between those two cases without adding another generic parameter,
8888
we just make the pragmatic assumption that the latter almost never happens.
8989
*/
90-
preloadedState?: PreloadedState<CombinedState<NoInfer<S>>>
90+
preloadedState?: PreloadedState
9191

9292
/**
9393
* The store enhancers to apply. See Redux's `createStore()`.
@@ -141,9 +141,12 @@ export type EnhancedStore<
141141
export function configureStore<
142142
S = any,
143143
A extends Action = AnyAction,
144+
PreloadedState = S,
144145
M extends Middlewares<S> = [ThunkMiddlewareFor<S>],
145146
E extends Enhancers = [StoreEnhancer]
146-
>(options: ConfigureStoreOptions<S, A, M, E>): EnhancedStore<S, A, M, E> {
147+
>(
148+
options: ConfigureStoreOptions<S, A, PreloadedState, M, E>
149+
): EnhancedStore<S, A, M, E> {
147150
const curriedGetDefaultMiddleware = curryGetDefaultMiddleware<S>()
148151

149152
const {
@@ -154,12 +157,16 @@ export function configureStore<
154157
enhancers = undefined,
155158
} = options || {}
156159

157-
let rootReducer: Reducer<S, A>
160+
let rootReducer: Reducer<S, A, PreloadedState>
158161

159162
if (typeof reducer === 'function') {
160163
rootReducer = reducer
161164
} else if (isPlainObject(reducer)) {
162-
rootReducer = combineReducers(reducer) as unknown as Reducer<S, A>
165+
rootReducer = combineReducers(reducer) as unknown as Reducer<
166+
S,
167+
A,
168+
PreloadedState
169+
>
163170
} else {
164171
throw new Error(
165172
'"reducer" is a required argument, and must be a function or an object of functions that can be passed to combineReducers'

packages/toolkit/src/tests/configureStore.typetest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ const _anyMiddleware: any = () => () => () => {}
131131
})
132132

133133
configureStore({
134-
reducer: () => 0,
135134
// @ts-expect-error
135+
reducer: (_: number) => 0,
136136
preloadedState: 'non-matching state type',
137137
})
138138
}

0 commit comments

Comments
 (0)