Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fulfillWithValue should infer return value #2888

Merged
merged 2 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions packages/toolkit/src/createAsyncThunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ export type BaseThunkAPI<
>
fulfillWithValue: IsUnknown<
FulfilledMeta,
<FulfilledValue>(
value: FulfilledValue
) => FulfillWithMeta<FulfilledValue, FulfilledMeta>,
<FulfilledValue>(value: FulfilledValue) => FulfilledValue,
<FulfilledValue>(
value: FulfilledValue,
meta: FulfilledMeta
Expand Down
44 changes: 38 additions & 6 deletions packages/toolkit/src/tests/createAsyncThunk.typetest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,38 @@ const anyAction = { type: 'foo' } as AnyAction
})
}

{
// https://github.com/reduxjs/redux-toolkit/issues/2886
// fulfillWithValue should infer return value

const initialState = {
loading: false,
obj: { magic: '' },
}

const getObj = createAsyncThunk(
'slice/getObj',
async (_: any, { fulfillWithValue, rejectWithValue }) => {
try {
return fulfillWithValue({ magic: 'object' })
} catch (rejected: any) {
return rejectWithValue(rejected?.response?.error || rejected)
}
}
)

createSlice({
name: 'slice',
initialState,
reducers: {},
extraReducers: (builder) => {
builder.addCase(getObj.fulfilled, (state, action) => {
expectExactType<{ magic: string }>(ANY)(action.payload)
})
},
})
}

// meta return values
{
// return values
Expand Down Expand Up @@ -621,8 +653,8 @@ const anyAction = { type: 'foo' } as AnyAction

// correct extra type
const { s, n } = api.extra
expectExactType<string>(s)
expectExactType<number>(n)
expectExactType<string>(ANY)(s)
expectExactType<number>(ANY)(n)

if (1 < 2)
// @ts-expect-error
Expand All @@ -646,8 +678,8 @@ const anyAction = { type: 'foo' } as AnyAction
})
// correct extra type
const { s, n } = api.extra
expectExactType<string>(s)
expectExactType<number>(n)
expectExactType<string>(ANY)(s)
expectExactType<number>(ANY)(n)

if (1 < 2)
// @ts-expect-error
Expand All @@ -673,8 +705,8 @@ const anyAction = { type: 'foo' } as AnyAction
})
// correct extra type
const { s, n } = api.extra
expectExactType<string>(s)
expectExactType<number>(n)
expectExactType<string>(ANY)(s)
expectExactType<number>(ANY)(n)
if (1 < 2) return api.rejectWithValue(5)
if (1 < 2)
// @ts-expect-error
Expand Down