Skip to content
Closed
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
2 changes: 1 addition & 1 deletion etc/redux-toolkit.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export function createAction<P = void, T extends string = string>(type: T): Payl
export function createAction<PA extends PrepareAction<any>, T extends string = string>(type: T, prepareAction: PA): PayloadActionCreator<ReturnType<PA>['payload'], T, PA>;

// @public (undocumented)
export function createAsyncThunk<Returned, ThunkArg = void, ThunkApiConfig extends AsyncThunkConfig = {}>(typePrefix: string, payloadCreator: AsyncThunkPayloadCreator<Returned, ThunkArg, ThunkApiConfig>, options?: AsyncThunkOptions<ThunkArg, ThunkApiConfig>): AsyncThunk<Returned, ThunkArg, ThunkApiConfig>;
export function createAsyncThunk<Returned, ThunkArg = OptionalUnknown, ThunkApiConfig extends AsyncThunkConfig = {}>(typePrefix: string, payloadCreator: AsyncThunkPayloadCreator<Returned, ThunkArg, ThunkApiConfig>, options?: AsyncThunkOptions<ThunkArg, ThunkApiConfig>): AsyncThunk<Returned, ThunkArg, ThunkApiConfig>;

// @public (undocumented)
export function createEntityAdapter<T>(options?: {
Expand Down
26 changes: 24 additions & 2 deletions src/createAsyncThunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ type AsyncThunkActionCreator<
? () => AsyncThunkAction<Returned, ThunkArg, ThunkApiConfig> // argument contains void
: [void] extends [ThunkArg] // make optional
? (arg?: ThunkArg) => AsyncThunkAction<Returned, ThunkArg, ThunkApiConfig> // argument contains undefined
: [ThunkArg] extends [OptionalUnknown]
? (arg?: unknown) => AsyncThunkAction<Returned, ThunkArg, ThunkApiConfig>
: [undefined] extends [ThunkArg]
? WithStrictNullChecks<
// with strict nullChecks: make optional
Expand Down Expand Up @@ -270,8 +272,28 @@ export type AsyncThunk<
typePrefix: string
}

const optionalUnknown = Symbol()
/*
* Since TypeScript 4.0, the following example will cause an error when checking
* JavaScript files (with allowJs & checkJs):
```js
const thunk = createAsyncThunk('', arg => {})
thunk()
// @ts-expect-error Expected 0 arguments, but got 1.ts(2554)
thunk('something')
```
* The only way around that without breaking too much explicit existing behaviour
* is this type "OptionalUnknown".
* On the flip side, using a payloadcreator without a defined argument, like
```ts
const thunk = createAsyncThunk('', () => {})
```
* will now generate a thunk that can optionally be called with any first argument
* opposed to a thunk that would not accept any argument before.
*/
type OptionalUnknown = { [optionalUnknown]: never }

/**
*
* @param typePrefix
* @param payloadCreator
* @param options
Expand All @@ -280,7 +302,7 @@ export type AsyncThunk<
*/
export function createAsyncThunk<
Returned,
ThunkArg = void,
ThunkArg = OptionalUnknown,
ThunkApiConfig extends AsyncThunkConfig = {}
>(
typePrefix: string,
Expand Down
3 changes: 2 additions & 1 deletion type-tests/files/createAsyncThunk.typetest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ const defaultDispatch = (() => {}) as ThunkDispatch<{}, any, AnyAction>
{
const asyncThunk = createAsyncThunk('test', () => 0)
expectType<() => any>(asyncThunk)
// @ts-expect-error cannot be called with an argument
// we have to allow anything to be passed in in this case, to allow
// for compatibility with allowJs & checkJS
asyncThunk(0 as any)
}

Expand Down