Skip to content

Commit b319c41

Browse files
authored
Merge pull request #2888 from /issues/2886
2 parents a56a194 + 73cd603 commit b319c41

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

packages/toolkit/src/createAsyncThunk.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ export type BaseThunkAPI<
3535
>
3636
fulfillWithValue: IsUnknown<
3737
FulfilledMeta,
38-
<FulfilledValue>(
39-
value: FulfilledValue
40-
) => FulfillWithMeta<FulfilledValue, FulfilledMeta>,
38+
<FulfilledValue>(value: FulfilledValue) => FulfilledValue,
4139
<FulfilledValue>(
4240
value: FulfilledValue,
4341
meta: FulfilledMeta

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)