Skip to content

Commit 8909f10

Browse files
committed
fulfillWithValue should infer return value
1 parent 026221a commit 8909f10

File tree

2 files changed

+56
-29
lines changed

2 files changed

+56
-29
lines changed

packages/toolkit/src/createAsyncThunk.ts

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -405,16 +405,18 @@ export type AsyncThunk<
405405
Returned,
406406
ThunkArg,
407407
ThunkApiConfig extends AsyncThunkConfig
408-
> = AsyncThunkActionCreator<Returned, ThunkArg, ThunkApiConfig> & {
409-
pending: AsyncThunkPendingActionCreator<ThunkArg, ThunkApiConfig>
410-
rejected: AsyncThunkRejectedActionCreator<ThunkArg, ThunkApiConfig>
411-
fulfilled: AsyncThunkFulfilledActionCreator<
412-
Returned,
413-
ThunkArg,
414-
ThunkApiConfig
415-
>
416-
typePrefix: string
417-
}
408+
> = [Returned] extends [FulfillWithMeta<infer P, infer M>]
409+
? AsyncThunk<P, ThunkArg, ThunkApiConfig & { fulfilledMeta: M }>
410+
: AsyncThunkActionCreator<Returned, ThunkArg, ThunkApiConfig> & {
411+
pending: AsyncThunkPendingActionCreator<ThunkArg, ThunkApiConfig>
412+
rejected: AsyncThunkRejectedActionCreator<ThunkArg, ThunkApiConfig>
413+
fulfilled: AsyncThunkFulfilledActionCreator<
414+
Returned,
415+
ThunkArg,
416+
ThunkApiConfig
417+
>
418+
typePrefix: string
419+
}
418420

419421
type OverrideThunkApiConfigs<OldConfig, NewConfig> = Id<
420422
NewConfig & Omit<OldConfig, keyof NewConfig>
@@ -694,19 +696,12 @@ If you want to use the AbortController to react to \`abort\` events, please cons
694696
}
695697
}
696698

697-
return Object.assign(
698-
actionCreator as AsyncThunkActionCreator<
699-
Returned,
700-
ThunkArg,
701-
ThunkApiConfig
702-
>,
703-
{
704-
pending,
705-
rejected,
706-
fulfilled,
707-
typePrefix,
708-
}
709-
)
699+
return Object.assign(actionCreator as any, {
700+
pending,
701+
rejected,
702+
fulfilled,
703+
typePrefix,
704+
})
710705
}
711706
createAsyncThunk.withTypes = () => createAsyncThunk
712707

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

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,38 @@ const anyAction = { type: 'foo' } as AnyAction
525525
})
526526
}
527527

528+
{
529+
// https://github.com/reduxjs/redux-toolkit/issues/2886
530+
// fulfillWithValue should infer return value
531+
532+
const initialState = {
533+
loading: false,
534+
obj: { magic: '' },
535+
}
536+
537+
const getObj = createAsyncThunk(
538+
'slice/getObj',
539+
async (_: any, { fulfillWithValue, rejectWithValue }) => {
540+
try {
541+
return fulfillWithValue({ magic: 'object' })
542+
} catch (rejected: any) {
543+
return rejectWithValue(rejected?.response?.error || rejected)
544+
}
545+
}
546+
)
547+
548+
createSlice({
549+
name: 'slice',
550+
initialState,
551+
reducers: {},
552+
extraReducers: (builder) => {
553+
builder.addCase(getObj.fulfilled, (state, action) => {
554+
expectExactType<{ magic: string }>(ANY)(action.payload)
555+
})
556+
},
557+
})
558+
}
559+
528560
// meta return values
529561
{
530562
// return values
@@ -621,8 +653,8 @@ const anyAction = { type: 'foo' } as AnyAction
621653

622654
// correct extra type
623655
const { s, n } = api.extra
624-
expectExactType<string>(s)
625-
expectExactType<number>(n)
656+
expectExactType<string>(ANY)(s)
657+
expectExactType<number>(ANY)(n)
626658

627659
if (1 < 2)
628660
// @ts-expect-error
@@ -646,8 +678,8 @@ const anyAction = { type: 'foo' } as AnyAction
646678
})
647679
// correct extra type
648680
const { s, n } = api.extra
649-
expectExactType<string>(s)
650-
expectExactType<number>(n)
681+
expectExactType<string>(ANY)(s)
682+
expectExactType<number>(ANY)(n)
651683

652684
if (1 < 2)
653685
// @ts-expect-error
@@ -673,8 +705,8 @@ const anyAction = { type: 'foo' } as AnyAction
673705
})
674706
// correct extra type
675707
const { s, n } = api.extra
676-
expectExactType<string>(s)
677-
expectExactType<number>(n)
708+
expectExactType<string>(ANY)(s)
709+
expectExactType<number>(ANY)(n)
678710
if (1 < 2) return api.rejectWithValue(5)
679711
if (1 < 2)
680712
// @ts-expect-error

0 commit comments

Comments
 (0)