Skip to content

Commit

Permalink
Prevent inference of any in create.asyncThunk
Browse files Browse the repository at this point in the history
  • Loading branch information
EskiMojo14 committed May 3, 2024
1 parent 7cd8142 commit 0007885
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
12 changes: 2 additions & 10 deletions packages/toolkit/src/createSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,15 +427,7 @@ export interface ReducerCreators<State> {
* @public
*/
export type SliceCaseReducers<State> =
| Record<
string,
| CaseReducerDefinition<State, PayloadAction<any>>
| CaseReducerWithPrepareDefinition<
State,
PayloadAction<any, string, any, any>
>
| AsyncThunkSliceReducerDefinition<State, any, any, any>
>
| Record<string, ReducerDefinition>
| Record<
string,
| CaseReducer<State, PayloadAction<any>>
Expand Down Expand Up @@ -692,7 +684,7 @@ export function buildCreateSlice({ creators }: BuildCreateSliceConfig = {}) {
} else {
handleNormalReducerDefinition<State>(
reducerDetails,
reducerDefinition,
reducerDefinition as any,
contextMethods,
)
}
Expand Down
38 changes: 38 additions & 0 deletions packages/toolkit/src/tests/createSlice.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,38 @@ describe('type tests', () => {
expectTypeOf(action.error).toEqualTypeOf<'error'>()
},
),
testInferVoid: create.asyncThunk(() => {}, {
pending(state, action) {
expectTypeOf(state).toEqualTypeOf<TestState>()

expectTypeOf(action.meta.arg).toBeVoid()
},
fulfilled(state, action) {
expectTypeOf(state).toEqualTypeOf<TestState>()

expectTypeOf(action.meta.arg).toBeVoid()

expectTypeOf(action.payload).toBeVoid()
},
rejected(state, action) {
expectTypeOf(state).toEqualTypeOf<TestState>()

expectTypeOf(action.meta.arg).toBeVoid()

expectTypeOf(action.error).toEqualTypeOf<SerializedError>()
},
settled(state, action) {
expectTypeOf(state).toEqualTypeOf<TestState>()

expectTypeOf(action.meta.arg).toBeVoid()

if (isRejected(action)) {
expectTypeOf(action.error).toEqualTypeOf<SerializedError>()
} else {
expectTypeOf(action.payload).toBeVoid()
}
},
}),
testInfer: create.asyncThunk(
function payloadCreator(arg: TestArg, api) {
return Promise.resolve<TestReturned>({ payload: 'foo' })
Expand Down Expand Up @@ -856,6 +888,12 @@ describe('type tests', () => {
>
>()

expectTypeOf(slice.actions.testInferVoid).toEqualTypeOf<
AsyncThunk<void, void, {}>
>()

expectTypeOf(slice.actions.testInferVoid).toBeCallableWith()

expectTypeOf(slice.actions.testInfer).toEqualTypeOf<
AsyncThunk<TestReturned, TestArg, {}>
>()
Expand Down

0 comments on commit 0007885

Please sign in to comment.