Skip to content

Commit

Permalink
Merge pull request #2629 from dokmic/bugfix/dispatch-type-inference
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson authored Oct 6, 2022
2 parents 7c3129c + 6687a38 commit 1cd4bb2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
39 changes: 39 additions & 0 deletions packages/toolkit/src/tests/configureStore.typetest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
configureStore,
getDefaultMiddleware,
createSlice,
ConfigureStoreOptions,
} from '@reduxjs/toolkit'
import type { ThunkMiddleware, ThunkAction, ThunkDispatch } from 'redux-thunk'
import thunk from 'redux-thunk'
Expand Down Expand Up @@ -304,6 +305,18 @@ const _anyMiddleware: any = () => () => () => {}
// @ts-expect-error
const result2: string = store.dispatch(5)
}
/**
* Test: read-only middleware tuple
*/
{
const store = configureStore({
reducer: reducerA,
middleware: [] as any as readonly [Middleware<(a: StateA) => boolean, StateA>],
})
const result: boolean = store.dispatch(5)
// @ts-expect-error
const result2: string = store.dispatch(5)
}
/**
* Test: multiple custom middleware
*/
Expand Down Expand Up @@ -473,6 +486,32 @@ const _anyMiddleware: any = () => () => () => {}
expectNotAny(store.dispatch)
}

/**
* Test: decorated `configureStore` won't make `dispatch` `never`
*/
{
const someSlice = createSlice({
name: 'something',
initialState: null as any,
reducers: {
set(state) {
return state;
},
},
});

function configureMyStore<S>(options: Omit<ConfigureStoreOptions<S>, 'reducer'>) {
return configureStore({
...options,
reducer: someSlice.reducer,
});
}

const store = configureMyStore({});

expectType<Function>(store.dispatch);
}

{
interface CounterState {
value: number
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/src/tsHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export type ExtractDispatchExtensions<M> = M extends MiddlewareArray<
infer MiddlewareTuple
>
? ExtractDispatchFromMiddlewareTuple<MiddlewareTuple, {}>
: M extends Middleware[]
: M extends ReadonlyArray<Middleware>
? ExtractDispatchFromMiddlewareTuple<[...M], {}>
: never

Expand Down

0 comments on commit 1cd4bb2

Please sign in to comment.