Skip to content

Commit

Permalink
Merge pull request #4667 from reduxjs/CAT-wrapper
Browse files Browse the repository at this point in the history
Add a type for `createAsyncThunk` without the `withTypes` method
  • Loading branch information
markerikson authored Nov 23, 2024
2 parents fe2d181 + f7d6ad9 commit dd8f986
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
15 changes: 10 additions & 5 deletions packages/toolkit/src/createAsyncThunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,9 @@ export type OverrideThunkApiConfigs<OldConfig, NewConfig> = Id<
NewConfig & Omit<OldConfig, keyof NewConfig>
>

type CreateAsyncThunk<CurriedThunkApiConfig extends AsyncThunkConfig> = {
export type CreateAsyncThunkFunction<
CurriedThunkApiConfig extends AsyncThunkConfig,
> = {
/**
*
* @param typePrefix
Expand Down Expand Up @@ -481,12 +483,15 @@ type CreateAsyncThunk<CurriedThunkApiConfig extends AsyncThunkConfig> = {
ThunkArg,
OverrideThunkApiConfigs<CurriedThunkApiConfig, ThunkApiConfig>
>

withTypes<ThunkApiConfig extends AsyncThunkConfig>(): CreateAsyncThunk<
OverrideThunkApiConfigs<CurriedThunkApiConfig, ThunkApiConfig>
>
}

type CreateAsyncThunk<CurriedThunkApiConfig extends AsyncThunkConfig> =
CreateAsyncThunkFunction<CurriedThunkApiConfig> & {
withTypes<ThunkApiConfig extends AsyncThunkConfig>(): CreateAsyncThunk<
OverrideThunkApiConfigs<CurriedThunkApiConfig, ThunkApiConfig>
>
}

export const createAsyncThunk = /* @__PURE__ */ (() => {
function createAsyncThunk<
Returned,
Expand Down
1 change: 1 addition & 0 deletions packages/toolkit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export type {
GetState,
GetThunkAPI,
SerializedError,
CreateAsyncThunkFunction,
} from './createAsyncThunk'

export {
Expand Down
23 changes: 22 additions & 1 deletion packages/toolkit/src/tests/createAsyncThunk.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { UnknownAction } from '@reduxjs/toolkit'
import type { CreateAsyncThunkFunction, UnknownAction } from '@reduxjs/toolkit'
import {
configureStore,
createAsyncThunk,
Expand Down Expand Up @@ -990,4 +990,25 @@ describe('meta', () => {
expect(thunk.settled).toEqual(expectFunction)
expect(thunk.fulfilled.type).toBe('a/fulfilled')
})
test('createAsyncThunkWrapper using CreateAsyncThunkFunction', async () => {
const customSerializeError = () => 'serialized!'
const createAppAsyncThunk: CreateAsyncThunkFunction<{
serializedErrorType: ReturnType<typeof customSerializeError>
}> = (prefix: string, payloadCreator: any, options: any) =>
createAsyncThunk(prefix, payloadCreator, {
...options,
serializeError: customSerializeError,
}) as any

const asyncThunk = createAppAsyncThunk('test', async () => {
throw new Error('Panic!')
})

const promise = store.dispatch(asyncThunk())
const result = await promise
if (!asyncThunk.rejected.match(result)) {
throw new Error('should have thrown')
}
expect(result.error).toEqual('serialized!')
})
})

0 comments on commit dd8f986

Please sign in to comment.