Skip to content

Commit 4259c4b

Browse files
committed
split off signature without AsyncThunkConfig for better inference
1 parent 5f3c105 commit 4259c4b

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

packages/toolkit/src/createAsyncThunk.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,21 @@ export type AsyncThunk<
415415
typePrefix: string
416416
}
417417

418+
/**
419+
*
420+
* @param typePrefix
421+
* @param payloadCreator
422+
* @param options
423+
*
424+
* @public
425+
*/
426+
// separate signature without `AsyncThunkConfig` for better inference
427+
export function createAsyncThunk<Returned, ThunkArg = void>(
428+
typePrefix: string,
429+
payloadCreator: AsyncThunkPayloadCreator<Returned, ThunkArg, {}>,
430+
options?: AsyncThunkOptions<ThunkArg, {}>
431+
): AsyncThunk<Returned, ThunkArg, {}>
432+
418433
/**
419434
*
420435
* @param typePrefix
@@ -425,8 +440,18 @@ export type AsyncThunk<
425440
*/
426441
export function createAsyncThunk<
427442
Returned,
428-
ThunkArg = void,
429-
ThunkApiConfig extends AsyncThunkConfig = {}
443+
ThunkArg,
444+
ThunkApiConfig extends AsyncThunkConfig
445+
>(
446+
typePrefix: string,
447+
payloadCreator: AsyncThunkPayloadCreator<Returned, ThunkArg, ThunkApiConfig>,
448+
options?: AsyncThunkOptions<ThunkArg, ThunkApiConfig>
449+
): AsyncThunk<Returned, ThunkArg, ThunkApiConfig>
450+
451+
export function createAsyncThunk<
452+
Returned,
453+
ThunkArg,
454+
ThunkApiConfig extends AsyncThunkConfig
430455
>(
431456
typePrefix: string,
432457
payloadCreator: AsyncThunkPayloadCreator<Returned, ThunkArg, ThunkApiConfig>,
@@ -533,7 +558,7 @@ If you want to use the AbortController to react to \`abort\` events, please cons
533558
return (dispatch, getState, extra) => {
534559
const requestId = options?.idGenerator
535560
? options.idGenerator(arg)
536-
: nanoid();
561+
: nanoid()
537562

538563
const abortController = new AC()
539564
let abortReason: string | undefined

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,27 +481,49 @@ const anyAction = { type: 'foo' } as AnyAction
481481
{
482482
// return values
483483
createAsyncThunk<'ret', void, {}>('test', (_, api) => 'ret' as const)
484+
createAsyncThunk<'ret', void, {}>('test', async (_, api) => 'ret' as const)
484485
createAsyncThunk<'ret', void, { fulfilledMeta: string }>('test', (_, api) =>
485486
api.fulfillWithValue('ret' as const, '')
486487
)
488+
createAsyncThunk<'ret', void, { fulfilledMeta: string }>(
489+
'test',
490+
async (_, api) => api.fulfillWithValue('ret' as const, '')
491+
)
487492
createAsyncThunk<'ret', void, { fulfilledMeta: string }>(
488493
'test',
489494
// @ts-expect-error has to be a fulfilledWithValue call
490495
(_, api) => 'ret' as const
491496
)
497+
createAsyncThunk<'ret', void, { fulfilledMeta: string }>(
498+
'test',
499+
// @ts-expect-error has to be a fulfilledWithValue call
500+
async (_, api) => 'ret' as const
501+
)
492502
createAsyncThunk<'ret', void, { fulfilledMeta: string }>(
493503
'test', // @ts-expect-error should only allow returning with 'test'
494504
(_, api) => api.fulfillWithValue(5, '')
495505
)
506+
createAsyncThunk<'ret', void, { fulfilledMeta: string }>(
507+
'test', // @ts-expect-error should only allow returning with 'test'
508+
async (_, api) => api.fulfillWithValue(5, '')
509+
)
496510

497511
// reject values
498512
createAsyncThunk<'ret', void, { rejectValue: string }>('test', (_, api) =>
499513
api.rejectWithValue('ret')
500514
)
515+
createAsyncThunk<'ret', void, { rejectValue: string }>(
516+
'test',
517+
async (_, api) => api.rejectWithValue('ret')
518+
)
501519
createAsyncThunk<'ret', void, { rejectValue: string; rejectedMeta: number }>(
502520
'test',
503521
(_, api) => api.rejectWithValue('ret', 5)
504522
)
523+
createAsyncThunk<'ret', void, { rejectValue: string; rejectedMeta: number }>(
524+
'test',
525+
async (_, api) => api.rejectWithValue('ret', 5)
526+
)
505527
createAsyncThunk<'ret', void, { rejectValue: string; rejectedMeta: number }>(
506528
'test',
507529
(_, api) => api.rejectWithValue('ret', 5)
@@ -511,9 +533,19 @@ const anyAction = { type: 'foo' } as AnyAction
511533
// @ts-expect-error wrong rejectedMeta type
512534
(_, api) => api.rejectWithValue('ret', '')
513535
)
536+
createAsyncThunk<'ret', void, { rejectValue: string; rejectedMeta: number }>(
537+
'test',
538+
// @ts-expect-error wrong rejectedMeta type
539+
async (_, api) => api.rejectWithValue('ret', '')
540+
)
514541
createAsyncThunk<'ret', void, { rejectValue: string; rejectedMeta: number }>(
515542
'test',
516543
// @ts-expect-error wrong rejectValue type
517544
(_, api) => api.rejectWithValue(5, '')
518545
)
546+
createAsyncThunk<'ret', void, { rejectValue: string; rejectedMeta: number }>(
547+
'test',
548+
// @ts-expect-error wrong rejectValue type
549+
async (_, api) => api.rejectWithValue(5, '')
550+
)
519551
}

0 commit comments

Comments
 (0)