Skip to content

Commit 0007885

Browse files
committed
Prevent inference of any in create.asyncThunk
1 parent 7cd8142 commit 0007885

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

Diff for: packages/toolkit/src/createSlice.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -427,15 +427,7 @@ export interface ReducerCreators<State> {
427427
* @public
428428
*/
429429
export type SliceCaseReducers<State> =
430-
| Record<
431-
string,
432-
| CaseReducerDefinition<State, PayloadAction<any>>
433-
| CaseReducerWithPrepareDefinition<
434-
State,
435-
PayloadAction<any, string, any, any>
436-
>
437-
| AsyncThunkSliceReducerDefinition<State, any, any, any>
438-
>
430+
| Record<string, ReducerDefinition>
439431
| Record<
440432
string,
441433
| CaseReducer<State, PayloadAction<any>>
@@ -692,7 +684,7 @@ export function buildCreateSlice({ creators }: BuildCreateSliceConfig = {}) {
692684
} else {
693685
handleNormalReducerDefinition<State>(
694686
reducerDetails,
695-
reducerDefinition,
687+
reducerDefinition as any,
696688
contextMethods,
697689
)
698690
}

Diff for: packages/toolkit/src/tests/createSlice.test-d.ts

+38
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,38 @@ describe('type tests', () => {
652652
expectTypeOf(action.error).toEqualTypeOf<'error'>()
653653
},
654654
),
655+
testInferVoid: create.asyncThunk(() => {}, {
656+
pending(state, action) {
657+
expectTypeOf(state).toEqualTypeOf<TestState>()
658+
659+
expectTypeOf(action.meta.arg).toBeVoid()
660+
},
661+
fulfilled(state, action) {
662+
expectTypeOf(state).toEqualTypeOf<TestState>()
663+
664+
expectTypeOf(action.meta.arg).toBeVoid()
665+
666+
expectTypeOf(action.payload).toBeVoid()
667+
},
668+
rejected(state, action) {
669+
expectTypeOf(state).toEqualTypeOf<TestState>()
670+
671+
expectTypeOf(action.meta.arg).toBeVoid()
672+
673+
expectTypeOf(action.error).toEqualTypeOf<SerializedError>()
674+
},
675+
settled(state, action) {
676+
expectTypeOf(state).toEqualTypeOf<TestState>()
677+
678+
expectTypeOf(action.meta.arg).toBeVoid()
679+
680+
if (isRejected(action)) {
681+
expectTypeOf(action.error).toEqualTypeOf<SerializedError>()
682+
} else {
683+
expectTypeOf(action.payload).toBeVoid()
684+
}
685+
},
686+
}),
655687
testInfer: create.asyncThunk(
656688
function payloadCreator(arg: TestArg, api) {
657689
return Promise.resolve<TestReturned>({ payload: 'foo' })
@@ -856,6 +888,12 @@ describe('type tests', () => {
856888
>
857889
>()
858890

891+
expectTypeOf(slice.actions.testInferVoid).toEqualTypeOf<
892+
AsyncThunk<void, void, {}>
893+
>()
894+
895+
expectTypeOf(slice.actions.testInferVoid).toBeCallableWith()
896+
859897
expectTypeOf(slice.actions.testInfer).toEqualTypeOf<
860898
AsyncThunk<TestReturned, TestArg, {}>
861899
>()

0 commit comments

Comments
 (0)