Skip to content

Commit

Permalink
Merge pull request #4389 from reduxjs/infer-void-async-thunk
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson authored May 9, 2024
2 parents 13ae28b + 10fae1a commit f17e122
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 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
7 changes: 3 additions & 4 deletions packages/toolkit/src/tests/createSlice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ describe('createSlice', () => {
initialState: [] as any[],
reducers: (create) => ({
thunkReducers: create.asyncThunk(
function payloadCreator(arg, api) {
function payloadCreator(arg: string, api) {
return Promise.resolve('resolved payload')
},
{ pending, fulfilled, rejected, settled },
Expand Down Expand Up @@ -722,7 +722,7 @@ describe('createSlice', () => {
reducers: (create) => ({
thunkReducers: create.asyncThunk(
// payloadCreator isn't allowed to return never
function payloadCreator(arg, api): any {
function payloadCreator(arg: string, api): any {
throw new Error('')
},
{ pending, fulfilled, rejected, settled },
Expand Down Expand Up @@ -765,7 +765,7 @@ describe('createSlice', () => {
initialState: [] as any[],
reducers: (create) => ({
thunkReducers: create.asyncThunk(
function payloadCreator(arg, api) {
function payloadCreator(arg: string, api) {
return 'should not call this'
},
{
Expand Down Expand Up @@ -833,7 +833,6 @@ describe('createSlice', () => {
slice.actions.thunkReducers.rejected(
new Error('test'),
'fakeRequestId',
{},
),
),
).not.toThrow()
Expand Down

0 comments on commit f17e122

Please sign in to comment.