Skip to content

Commit dd8f986

Browse files
authored
Merge pull request #4667 from reduxjs/CAT-wrapper
Add a type for `createAsyncThunk` without the `withTypes` method
2 parents fe2d181 + f7d6ad9 commit dd8f986

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

packages/toolkit/src/createAsyncThunk.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,9 @@ export type OverrideThunkApiConfigs<OldConfig, NewConfig> = Id<
437437
NewConfig & Omit<OldConfig, keyof NewConfig>
438438
>
439439

440-
type CreateAsyncThunk<CurriedThunkApiConfig extends AsyncThunkConfig> = {
440+
export type CreateAsyncThunkFunction<
441+
CurriedThunkApiConfig extends AsyncThunkConfig,
442+
> = {
441443
/**
442444
*
443445
* @param typePrefix
@@ -481,12 +483,15 @@ type CreateAsyncThunk<CurriedThunkApiConfig extends AsyncThunkConfig> = {
481483
ThunkArg,
482484
OverrideThunkApiConfigs<CurriedThunkApiConfig, ThunkApiConfig>
483485
>
484-
485-
withTypes<ThunkApiConfig extends AsyncThunkConfig>(): CreateAsyncThunk<
486-
OverrideThunkApiConfigs<CurriedThunkApiConfig, ThunkApiConfig>
487-
>
488486
}
489487

488+
type CreateAsyncThunk<CurriedThunkApiConfig extends AsyncThunkConfig> =
489+
CreateAsyncThunkFunction<CurriedThunkApiConfig> & {
490+
withTypes<ThunkApiConfig extends AsyncThunkConfig>(): CreateAsyncThunk<
491+
OverrideThunkApiConfigs<CurriedThunkApiConfig, ThunkApiConfig>
492+
>
493+
}
494+
490495
export const createAsyncThunk = /* @__PURE__ */ (() => {
491496
function createAsyncThunk<
492497
Returned,

packages/toolkit/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ export type {
133133
GetState,
134134
GetThunkAPI,
135135
SerializedError,
136+
CreateAsyncThunkFunction,
136137
} from './createAsyncThunk'
137138

138139
export {

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { UnknownAction } from '@reduxjs/toolkit'
1+
import type { CreateAsyncThunkFunction, UnknownAction } from '@reduxjs/toolkit'
22
import {
33
configureStore,
44
createAsyncThunk,
@@ -990,4 +990,25 @@ describe('meta', () => {
990990
expect(thunk.settled).toEqual(expectFunction)
991991
expect(thunk.fulfilled.type).toBe('a/fulfilled')
992992
})
993+
test('createAsyncThunkWrapper using CreateAsyncThunkFunction', async () => {
994+
const customSerializeError = () => 'serialized!'
995+
const createAppAsyncThunk: CreateAsyncThunkFunction<{
996+
serializedErrorType: ReturnType<typeof customSerializeError>
997+
}> = (prefix: string, payloadCreator: any, options: any) =>
998+
createAsyncThunk(prefix, payloadCreator, {
999+
...options,
1000+
serializeError: customSerializeError,
1001+
}) as any
1002+
1003+
const asyncThunk = createAppAsyncThunk('test', async () => {
1004+
throw new Error('Panic!')
1005+
})
1006+
1007+
const promise = store.dispatch(asyncThunk())
1008+
const result = await promise
1009+
if (!asyncThunk.rejected.match(result)) {
1010+
throw new Error('should have thrown')
1011+
}
1012+
expect(result.error).toEqual('serialized!')
1013+
})
9931014
})

0 commit comments

Comments
 (0)