Skip to content

Commit

Permalink
Merge pull request #2888 from /issues/2886
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson committed Nov 30, 2022
2 parents a56a194 + 73cd603 commit b319c41
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
4 changes: 1 addition & 3 deletions packages/toolkit/src/createAsyncThunk.ts
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
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

0 comments on commit b319c41

Please sign in to comment.