Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions docs/api/configureStore.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ Redux Toolkit's `configureStore` simplifies that setup process, by doing all tha

interface ConfigureStoreOptions<
S = any,
A extends Action = UnknownAction,
M extends Tuple<Middlewares<S>> = Tuple<Middlewares<S>>
E extends Tuple<Enhancers> = Tuple<Enhancers>,
P = S
Expand All @@ -49,13 +48,16 @@ interface ConfigureStoreOptions<
* 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, P> | ReducersMapObject<S, A, P>
reducer: Reducer<S, UnknownAction, P> | ReducersMapObject<S, UnknownAction, P>

/**
* An array of Redux middleware to install. If not supplied, defaults to
* the set of middleware returned by `getDefaultMiddleware()`.
* A callback which receives `getDefaultMiddleware` and should return a Tuple
* of middleware to install. If not supplied, defaults to the set of
* middleware returned by `getDefaultMiddleware()`.
*
* @example `middleware: (gDM) => gDM().concat(logger, apiMiddleware, yourCustomMiddleware)`
*/
middleware?: ((getDefaultMiddleware: CurriedGetDefaultMiddleware<S>) => M) | M
middleware?: (getDefaultMiddleware: GetDefaultMiddleware<S>) => M

/**
* Whether to enable Redux DevTools integration. Defaults to `true`.
Expand Down Expand Up @@ -87,16 +89,15 @@ interface ConfigureStoreOptions<
* and should return a new array (such as `getDefaultEnhancers().concat(offline)`).
* If you only need to add middleware, you can use the `middleware` parameter instead.
*/
enhancers?: (getDefaultEnhancers: GetDefaultEnhancers<M>) => E | E
enhancers?: (getDefaultEnhancers: GetDefaultEnhancers<M>) => E
}

function configureStore<
S = any,
A extends Action = UnknownAction,
M extends Tuple<Middlewares<S>> = Tuple<Middlewares<S>>
E extends Tuple<Enhancers> = Tuple<Enhancers>,
P = S
>(options: ConfigureStoreOptions<S, A, M, E, P>): EnhancedStore<S, A, M, E>
>(options: ConfigureStoreOptions<S, M, E, P>): EnhancedStore<S, E>
```

### `reducer`
Expand Down
23 changes: 14 additions & 9 deletions packages/toolkit/src/configureStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type {
Reducer,
ReducersMapObject,
Middleware,
Action,
StoreEnhancer,
Store,
UnknownAction,
Expand Down Expand Up @@ -39,16 +38,20 @@ import { buildGetDefaultEnhancers } from './getDefaultEnhancers'
*/
export interface ConfigureStoreOptions<
S = any,
A extends Action = UnknownAction,
M extends Tuple<Middlewares<S>> = Tuple<Middlewares<S>>,
E extends Tuple<Enhancers> = Tuple<Enhancers>,
P = S,
> {
/**
* A single reducer function that will be used as the root reducer, or an
* object of slice reducers that will be passed to `combineReducers()`.
*
* The reducer must handle `UnknownAction`, matching the store's dispatch
* type. A root reducer is always called with actions it doesn't recognise
* (`@@INIT`, replaced-reducer actions, thunk-dispatched actions, ...), so
* its `action` parameter cannot be narrowed to a specific action type.
*/
reducer: Reducer<S, A, P> | ReducersMapObject<S, A, P>
reducer: Reducer<S, UnknownAction, P> | ReducersMapObject<S, UnknownAction, P>

/**
* An array of Redux middleware to install, or a callback receiving `getDefaultMiddleware` and returning a Tuple of middleware.
Expand Down Expand Up @@ -105,10 +108,9 @@ type Enhancers = ReadonlyArray<StoreEnhancer>
*/
export type EnhancedStore<
S = any,
A extends Action = UnknownAction,
E extends Enhancers = Enhancers,
> = ExtractStoreExtensions<E> &
Store<S, A, UnknownIfNonSpecific<ExtractStateExtensions<E>>>
Store<S, UnknownAction, UnknownIfNonSpecific<ExtractStateExtensions<E>>>

/**
* A friendly abstraction over the standard Redux `createStore()` function.
Expand All @@ -120,13 +122,12 @@ export type EnhancedStore<
*/
export function configureStore<
S = any,
A extends Action = UnknownAction,
M extends Tuple<Middlewares<S>> = Tuple<[ThunkMiddlewareFor<S>]>,
E extends Tuple<Enhancers> = Tuple<
[StoreEnhancer<{ dispatch: ExtractDispatchExtensions<M> }>, StoreEnhancer]
>,
P = S,
>(options: ConfigureStoreOptions<S, A, M, E, P>): EnhancedStore<S, A, E> {
>(options: ConfigureStoreOptions<S, M, E, P>): EnhancedStore<S, E> {
const getDefaultMiddleware = buildGetDefaultMiddleware<S>()

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

let rootReducer: Reducer<S, A, P>
let rootReducer: Reducer<S, UnknownAction, P>

if (typeof reducer === 'function') {
rootReducer = reducer
} else if (isPlainObject(reducer)) {
rootReducer = combineReducers(reducer) as unknown as Reducer<S, A, P>
rootReducer = combineReducers(reducer) as unknown as Reducer<
S,
UnknownAction,
P
>
} 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
37 changes: 33 additions & 4 deletions packages/toolkit/src/tests/configureStore.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,43 @@ describe('type tests', () => {
expectTypeOf(store).not.toExtend<Store<string, UnknownAction>>()
})

test('configureStore() infers the store action type.', () => {
const reducer: Reducer<number, PayloadAction<number>> = () => 0
test('configureStore() always types the store action as `UnknownAction`.', () => {
// `configureStore` no longer accepts/infers a store-wide action type - the
// store always dispatches `UnknownAction`, matching the root reducer, which
// is always called with actions it doesn't recognise.
// See https://github.com/reduxjs/redux-toolkit/issues/5317
const reducer: Reducer<number, UnknownAction> = () => 0

const store = configureStore({ reducer })

expectTypeOf(store).toExtend<Store<number, PayloadAction<number>>>()
expectTypeOf(store).toExtend<Store<number, UnknownAction>>()

expectTypeOf(store).not.toExtend<Store<number, PayloadAction<number>>>()
})

test('configureStore() rejects a reducer narrowed to a specific action.', () => {
// A root reducer is always called with `@@INIT`, replaced-reducer actions,
// thunk-dispatched actions, etc. - narrowing its `action` parameter below
// `UnknownAction` is unsound, so the `reducer` option rejects it.
const specificReducer: Reducer<number, PayloadAction<number>> = () => 0

// @ts-expect-error the reducer must handle `UnknownAction`
configureStore({ reducer: specificReducer })

// slice reducers and `combineReducers()` output handle `UnknownAction`, so
// they remain accepted.
const slice = createSlice({
name: 'counter',
initialState: 0,
reducers: {
increment: (state, action: PayloadAction<number>) =>
state + action.payload,
},
})

configureStore({ reducer: slice.reducer })

expectTypeOf(store).not.toExtend<Store<number, PayloadAction<string>>>()
configureStore({ reducer: combineReducers({ counter: slice.reducer }) })
})

test('configureStore() accepts Tuple for middleware, but not plain array.', () => {
Expand Down
5 changes: 1 addition & 4 deletions packages/toolkit/src/tests/utils/helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,7 @@ export function setupApiStore<
} & {
[K in keyof R]: ReturnType<R[K]>
},
UnknownAction,
ReturnType<typeof getStore> extends EnhancedStore<any, any, infer M>
? M
: never
ReturnType<typeof getStore> extends EnhancedStore<any, infer M> ? M : never
>

const initialStore = getStore() as StoreType
Expand Down
Loading