Skip to content

Commit 12d5797

Browse files
bfrickaBrian Frichette
andauthored
Add test for #1456 (#1461)
- Test `createAsyncThunk` + `rejectWithValue` without generics. This test works in 1.5.1 but not in 1.6.0 - 1.6.1 Co-authored-by: Brian Frichette <[email protected]>
1 parent 544b975 commit 12d5797

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

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

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import type { AxiosError } from 'axios'
77
import apiRequest from 'axios'
88
import type { IsAny, IsUnknown } from '@internal/tsHelpers'
99
import { expectType } from './helpers'
10-
import { AsyncThunkPayloadCreator } from '@internal/createAsyncThunk'
10+
import type {
11+
AsyncThunkFulfilledActionCreator,
12+
AsyncThunkRejectedActionCreator,
13+
} from '@internal/createAsyncThunk'
1114

1215
const defaultDispatch = (() => {}) as ThunkDispatch<{}, any, AnyAction>
1316
const anyAction = { type: 'foo' } as AnyAction
@@ -422,6 +425,44 @@ const anyAction = { type: 'foo' } as AnyAction
422425
expectType<AsyncThunk<'ret', void, {}>>(thunk)
423426
}
424427

428+
// createAsyncThunk rejectWithValue without generics: Expect correct return type
429+
{
430+
const asyncThunk = createAsyncThunk(
431+
'test',
432+
(_: void, { rejectWithValue }) => {
433+
try {
434+
return Promise.resolve(true)
435+
} catch (e) {
436+
return rejectWithValue(e)
437+
}
438+
}
439+
)
440+
441+
defaultDispatch(asyncThunk())
442+
.then((result) => {
443+
if (asyncThunk.fulfilled.match(result)) {
444+
expectType<ReturnType<AsyncThunkFulfilledActionCreator<boolean, void>>>(
445+
result
446+
)
447+
expectType<boolean>(result.payload)
448+
// @ts-expect-error
449+
expectType<any>(result.error)
450+
} else {
451+
expectType<ReturnType<AsyncThunkRejectedActionCreator<unknown, void>>>(
452+
result
453+
)
454+
expectType<SerializedError>(result.error)
455+
expectType<unknown>(result.payload)
456+
}
457+
458+
return result
459+
})
460+
.then(unwrapResult)
461+
.then((unwrapped) => {
462+
expectType<boolean>(unwrapped)
463+
})
464+
}
465+
425466
{
426467
type Funky = { somethingElse: 'Funky!' }
427468
function funkySerializeError(err: any): Funky {

0 commit comments

Comments
 (0)