Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a type for createAsyncThunk without the withTypes method #4667

Merged
merged 2 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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!')
})
})