Skip to content

Commit 5384159

Browse files
EskiMojo14markerikson
authored andcommitted
Add a CreateAsyncThunkWithoutWithTypes type
1 parent fe2d181 commit 5384159

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-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 CreateAsyncThunkWithoutWithTypes<
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+
CreateAsyncThunkWithoutWithTypes<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+
CreateAsyncThunkWithoutWithTypes,
136137
} from './createAsyncThunk'
137138

138139
export {

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

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

0 commit comments

Comments
 (0)