diff --git a/packages/toolkit/src/createAsyncThunk.ts b/packages/toolkit/src/createAsyncThunk.ts index 4534165869..897edf73bd 100644 --- a/packages/toolkit/src/createAsyncThunk.ts +++ b/packages/toolkit/src/createAsyncThunk.ts @@ -437,7 +437,9 @@ export type OverrideThunkApiConfigs = Id< NewConfig & Omit > -type CreateAsyncThunk = { +export type CreateAsyncThunkWithoutWithTypes< + CurriedThunkApiConfig extends AsyncThunkConfig, +> = { /** * * @param typePrefix @@ -481,12 +483,15 @@ type CreateAsyncThunk = { ThunkArg, OverrideThunkApiConfigs > - - withTypes(): CreateAsyncThunk< - OverrideThunkApiConfigs - > } +type CreateAsyncThunk = + CreateAsyncThunkWithoutWithTypes & { + withTypes(): CreateAsyncThunk< + OverrideThunkApiConfigs + > + } + export const createAsyncThunk = /* @__PURE__ */ (() => { function createAsyncThunk< Returned, diff --git a/packages/toolkit/src/index.ts b/packages/toolkit/src/index.ts index 1883310dc2..ef234c3fce 100644 --- a/packages/toolkit/src/index.ts +++ b/packages/toolkit/src/index.ts @@ -133,6 +133,7 @@ export type { GetState, GetThunkAPI, SerializedError, + CreateAsyncThunkWithoutWithTypes, } from './createAsyncThunk' export { diff --git a/packages/toolkit/src/tests/createAsyncThunk.test.ts b/packages/toolkit/src/tests/createAsyncThunk.test.ts index 971ee3682a..64bfe6c773 100644 --- a/packages/toolkit/src/tests/createAsyncThunk.test.ts +++ b/packages/toolkit/src/tests/createAsyncThunk.test.ts @@ -1,4 +1,7 @@ -import type { UnknownAction } from '@reduxjs/toolkit' +import type { + CreateAsyncThunkWithoutWithTypes, + UnknownAction, +} from '@reduxjs/toolkit' import { configureStore, createAsyncThunk, @@ -990,4 +993,25 @@ describe('meta', () => { expect(thunk.settled).toEqual(expectFunction) expect(thunk.fulfilled.type).toBe('a/fulfilled') }) + test('createAsyncThunkWrapper using CreateAsyncThunkWithoutWithTypes', async () => { + const customSerializeError = () => 'serialized!' + const createAppAsyncThunk: CreateAsyncThunkWithoutWithTypes<{ + serializedErrorType: ReturnType + }> = (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!') + }) })