Skip to content

Commit

Permalink
split off signature without AsyncThunkConfig for better inference
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Oct 24, 2021
1 parent 5f3c105 commit 4259c4b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
31 changes: 28 additions & 3 deletions packages/toolkit/src/createAsyncThunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,21 @@ export type AsyncThunk<
typePrefix: string
}

/**
*
* @param typePrefix
* @param payloadCreator
* @param options
*
* @public
*/
// separate signature without `AsyncThunkConfig` for better inference
export function createAsyncThunk<Returned, ThunkArg = void>(
typePrefix: string,
payloadCreator: AsyncThunkPayloadCreator<Returned, ThunkArg, {}>,
options?: AsyncThunkOptions<ThunkArg, {}>
): AsyncThunk<Returned, ThunkArg, {}>

/**
*
* @param typePrefix
Expand All @@ -425,8 +440,18 @@ export type AsyncThunk<
*/
export function createAsyncThunk<
Returned,
ThunkArg = void,
ThunkApiConfig extends AsyncThunkConfig = {}
ThunkArg,
ThunkApiConfig extends AsyncThunkConfig
>(
typePrefix: string,
payloadCreator: AsyncThunkPayloadCreator<Returned, ThunkArg, ThunkApiConfig>,
options?: AsyncThunkOptions<ThunkArg, ThunkApiConfig>
): AsyncThunk<Returned, ThunkArg, ThunkApiConfig>

export function createAsyncThunk<
Returned,
ThunkArg,
ThunkApiConfig extends AsyncThunkConfig
>(
typePrefix: string,
payloadCreator: AsyncThunkPayloadCreator<Returned, ThunkArg, ThunkApiConfig>,
Expand Down Expand Up @@ -533,7 +558,7 @@ If you want to use the AbortController to react to \`abort\` events, please cons
return (dispatch, getState, extra) => {
const requestId = options?.idGenerator
? options.idGenerator(arg)
: nanoid();
: nanoid()

const abortController = new AC()
let abortReason: string | undefined
Expand Down
32 changes: 32 additions & 0 deletions packages/toolkit/src/tests/createAsyncThunk.typetest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,27 +481,49 @@ const anyAction = { type: 'foo' } as AnyAction
{
// return values
createAsyncThunk<'ret', void, {}>('test', (_, api) => 'ret' as const)
createAsyncThunk<'ret', void, {}>('test', async (_, api) => 'ret' as const)
createAsyncThunk<'ret', void, { fulfilledMeta: string }>('test', (_, api) =>
api.fulfillWithValue('ret' as const, '')
)
createAsyncThunk<'ret', void, { fulfilledMeta: string }>(
'test',
async (_, api) => api.fulfillWithValue('ret' as const, '')
)
createAsyncThunk<'ret', void, { fulfilledMeta: string }>(
'test',
// @ts-expect-error has to be a fulfilledWithValue call
(_, api) => 'ret' as const
)
createAsyncThunk<'ret', void, { fulfilledMeta: string }>(
'test',
// @ts-expect-error has to be a fulfilledWithValue call
async (_, api) => 'ret' as const
)
createAsyncThunk<'ret', void, { fulfilledMeta: string }>(
'test', // @ts-expect-error should only allow returning with 'test'
(_, api) => api.fulfillWithValue(5, '')
)
createAsyncThunk<'ret', void, { fulfilledMeta: string }>(
'test', // @ts-expect-error should only allow returning with 'test'
async (_, api) => api.fulfillWithValue(5, '')
)

// reject values
createAsyncThunk<'ret', void, { rejectValue: string }>('test', (_, api) =>
api.rejectWithValue('ret')
)
createAsyncThunk<'ret', void, { rejectValue: string }>(
'test',
async (_, api) => api.rejectWithValue('ret')
)
createAsyncThunk<'ret', void, { rejectValue: string; rejectedMeta: number }>(
'test',
(_, api) => api.rejectWithValue('ret', 5)
)
createAsyncThunk<'ret', void, { rejectValue: string; rejectedMeta: number }>(
'test',
async (_, api) => api.rejectWithValue('ret', 5)
)
createAsyncThunk<'ret', void, { rejectValue: string; rejectedMeta: number }>(
'test',
(_, api) => api.rejectWithValue('ret', 5)
Expand All @@ -511,9 +533,19 @@ const anyAction = { type: 'foo' } as AnyAction
// @ts-expect-error wrong rejectedMeta type
(_, api) => api.rejectWithValue('ret', '')
)
createAsyncThunk<'ret', void, { rejectValue: string; rejectedMeta: number }>(
'test',
// @ts-expect-error wrong rejectedMeta type
async (_, api) => api.rejectWithValue('ret', '')
)
createAsyncThunk<'ret', void, { rejectValue: string; rejectedMeta: number }>(
'test',
// @ts-expect-error wrong rejectValue type
(_, api) => api.rejectWithValue(5, '')
)
createAsyncThunk<'ret', void, { rejectValue: string; rejectedMeta: number }>(
'test',
// @ts-expect-error wrong rejectValue type
async (_, api) => api.rejectWithValue(5, '')
)
}

0 comments on commit 4259c4b

Please sign in to comment.