Skip to content

Commit 1cd4bb2

Browse files
authored
Merge pull request #2629 from dokmic/bugfix/dispatch-type-inference
2 parents 7c3129c + 6687a38 commit 1cd4bb2

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
configureStore,
1414
getDefaultMiddleware,
1515
createSlice,
16+
ConfigureStoreOptions,
1617
} from '@reduxjs/toolkit'
1718
import type { ThunkMiddleware, ThunkAction, ThunkDispatch } from 'redux-thunk'
1819
import thunk from 'redux-thunk'
@@ -304,6 +305,18 @@ const _anyMiddleware: any = () => () => () => {}
304305
// @ts-expect-error
305306
const result2: string = store.dispatch(5)
306307
}
308+
/**
309+
* Test: read-only middleware tuple
310+
*/
311+
{
312+
const store = configureStore({
313+
reducer: reducerA,
314+
middleware: [] as any as readonly [Middleware<(a: StateA) => boolean, StateA>],
315+
})
316+
const result: boolean = store.dispatch(5)
317+
// @ts-expect-error
318+
const result2: string = store.dispatch(5)
319+
}
307320
/**
308321
* Test: multiple custom middleware
309322
*/
@@ -473,6 +486,32 @@ const _anyMiddleware: any = () => () => () => {}
473486
expectNotAny(store.dispatch)
474487
}
475488

489+
/**
490+
* Test: decorated `configureStore` won't make `dispatch` `never`
491+
*/
492+
{
493+
const someSlice = createSlice({
494+
name: 'something',
495+
initialState: null as any,
496+
reducers: {
497+
set(state) {
498+
return state;
499+
},
500+
},
501+
});
502+
503+
function configureMyStore<S>(options: Omit<ConfigureStoreOptions<S>, 'reducer'>) {
504+
return configureStore({
505+
...options,
506+
reducer: someSlice.reducer,
507+
});
508+
}
509+
510+
const store = configureMyStore({});
511+
512+
expectType<Function>(store.dispatch);
513+
}
514+
476515
{
477516
interface CounterState {
478517
value: number

packages/toolkit/src/tsHelpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export type ExtractDispatchExtensions<M> = M extends MiddlewareArray<
8888
infer MiddlewareTuple
8989
>
9090
? ExtractDispatchFromMiddlewareTuple<MiddlewareTuple, {}>
91-
: M extends Middleware[]
91+
: M extends ReadonlyArray<Middleware>
9292
? ExtractDispatchFromMiddlewareTuple<[...M], {}>
9393
: never
9494

0 commit comments

Comments
 (0)