Skip to content

Commit

Permalink
Update types to work with the PreloadedState generic
Browse files Browse the repository at this point in the history
  • Loading branch information
Methuselah96 committed Feb 22, 2023
1 parent 65902f5 commit 8e2928c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
23 changes: 15 additions & 8 deletions packages/toolkit/src/configureStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import type {
StoreEnhancer,
Store,
Dispatch,
PreloadedState,
CombinedState,
} from 'redux'
import { createStore, compose, applyMiddleware, combineReducers } from 'redux'
import type { DevToolsEnhancerOptions as DevToolsOptions } from './devtoolsExtension'
Expand All @@ -21,7 +19,6 @@ import type {
} from './getDefaultMiddleware'
import { curryGetDefaultMiddleware } from './getDefaultMiddleware'
import type {
NoInfer,
ExtractDispatchExtensions,
ExtractStoreExtensions,
} from './tsHelpers'
Expand All @@ -45,14 +42,17 @@ export type ConfigureEnhancersCallback<E extends Enhancers = Enhancers> = (
export interface ConfigureStoreOptions<
S = any,
A extends Action = AnyAction,
PreloadedState = S,
M extends Middlewares<S> = Middlewares<S>,
E extends Enhancers = Enhancers
> {
/**
* A single reducer function that will be used as the root reducer, or an
* object of slice reducers that will be passed to `combineReducers()`.
*/
reducer: Reducer<S, A> | ReducersMapObject<S, A>
reducer:
| Reducer<S, A, PreloadedState>
| ReducersMapObject<S, A, PreloadedState>

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

/**
* The store enhancers to apply. See Redux's `createStore()`.
Expand Down Expand Up @@ -141,9 +141,12 @@ export type EnhancedStore<
export function configureStore<
S = any,
A extends Action = AnyAction,
PreloadedState = S,
M extends Middlewares<S> = [ThunkMiddlewareFor<S>],
E extends Enhancers = [StoreEnhancer]
>(options: ConfigureStoreOptions<S, A, M, E>): EnhancedStore<S, A, M, E> {
>(
options: ConfigureStoreOptions<S, A, PreloadedState, M, E>
): EnhancedStore<S, A, M, E> {
const curriedGetDefaultMiddleware = curryGetDefaultMiddleware<S>()

const {
Expand All @@ -154,12 +157,16 @@ export function configureStore<
enhancers = undefined,
} = options || {}

let rootReducer: Reducer<S, A>
let rootReducer: Reducer<S, A, PreloadedState>

if (typeof reducer === 'function') {
rootReducer = reducer
} else if (isPlainObject(reducer)) {
rootReducer = combineReducers(reducer) as unknown as Reducer<S, A>
rootReducer = combineReducers(reducer) as unknown as Reducer<
S,
A,
PreloadedState
>
} else {
throw new Error(
'"reducer" is a required argument, and must be a function or an object of functions that can be passed to combineReducers'
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/src/tests/configureStore.typetest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ const _anyMiddleware: any = () => () => () => {}
})

configureStore({
reducer: () => 0,
// @ts-expect-error
reducer: (_: number) => 0,
preloadedState: 'non-matching state type',
})
}
Expand Down

0 comments on commit 8e2928c

Please sign in to comment.