Skip to content

Commit

Permalink
Add a CreateAsyncThunkWithoutWithTypes type
Browse files Browse the repository at this point in the history
  • Loading branch information
EskiMojo14 committed Oct 16, 2024
1 parent 7af5345 commit cccc8bc
Show file tree
Hide file tree
Showing 3 changed files with 36 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 CreateAsyncThunkWithoutWithTypes<
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> =
CreateAsyncThunkWithoutWithTypes<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,
CreateAsyncThunkWithoutWithTypes,
} from './createAsyncThunk'

export {
Expand Down
26 changes: 25 additions & 1 deletion packages/toolkit/src/tests/createAsyncThunk.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { UnknownAction } from '@reduxjs/toolkit'
import type {
CreateAsyncThunkWithoutWithTypes,
UnknownAction,
} from '@reduxjs/toolkit'
import {
configureStore,
createAsyncThunk,
Expand Down Expand Up @@ -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<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 cccc8bc

Please sign in to comment.