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

simplify signatures of ActionCreatorWithOptionalPayload and `A… #428

Merged
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
4 changes: 1 addition & 3 deletions etc/redux-toolkit.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ export interface ActionCreatorWithNonInferrablePayload<T extends string = string

// @public
export interface ActionCreatorWithOptionalPayload<P, T extends string = string> extends BaseActionCreator<P, T> {
(payload?: undefined): PayloadAction<undefined, T>;
<PT extends Diff<P, undefined>>(payload?: PT): PayloadAction<PT, T>;
(payload?: P): PayloadAction<P, T>;
}

// @public
Expand All @@ -39,7 +38,6 @@ export interface ActionCreatorWithoutPayload<T extends string = string> extends

// @public
export interface ActionCreatorWithPayload<P, T extends string = string> extends BaseActionCreator<P, T> {
<PT extends P>(payload: PT): PayloadAction<PT, T>;
(payload: P): PayloadAction<P, T>;
}

Expand Down
18 changes: 3 additions & 15 deletions src/createAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,12 @@ export interface ActionCreatorWithPreparedPayload<
*/
export interface ActionCreatorWithOptionalPayload<P, T extends string = string>
extends BaseActionCreator<P, T> {
/**
* Calling this {@link redux#ActionCreator} without arguments will
* return a {@link PayloadAction} of type `T` with a payload of `undefined`
*/
(payload?: undefined): PayloadAction<undefined, T>
/**
* Calling this {@link redux#ActionCreator} with an argument will
* return a {@link PayloadAction} of type `T` with a payload of `P`
* return a {@link PayloadAction} of type `T` with a payload of `P`.
* Calling it without an argument will return a PayloadAction with a payload of `undefined`.
*/
<PT extends Diff<P, undefined>>(payload?: PT): PayloadAction<PT, T>
(payload?: P): PayloadAction<P, T>
}

/**
Expand Down Expand Up @@ -160,12 +156,6 @@ export interface ActionCreatorWithoutPayload<T extends string = string>
*/
export interface ActionCreatorWithPayload<P, T extends string = string>
extends BaseActionCreator<P, T> {
/**
* Calling this {@link redux#ActionCreator} with an argument will
* return a {@link PayloadAction} of type `T` with a payload of `P`
* If possible, `P` will be narrowed down to the exact type of the payload argument.
*/
<PT extends P>(payload: PT): PayloadAction<PT, T>
/**
* Calling this {@link redux#ActionCreator} with an argument will
* return a {@link PayloadAction} of type `T` with a payload of `P`
Expand Down Expand Up @@ -333,8 +323,6 @@ export function getType<T extends string>(

// helper types for more readable typings

type Diff<T, U> = T extends U ? never : T

type IfPrepareActionMethodProvided<
PA extends PrepareAction<any> | void,
True,
Expand Down
12 changes: 6 additions & 6 deletions type-tests/files/createAction.typetest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ function expectType<T>(p: T): T {
{ type: 'action' }
) as PayloadActionCreator<number | undefined>

expectType<PayloadAction<number>>(actionCreator(1))
expectType<PayloadAction<undefined>>(actionCreator())
expectType<PayloadAction<undefined>>(actionCreator(undefined))
expectType<PayloadAction<number | undefined>>(actionCreator(1))
expectType<PayloadAction<number | undefined>>(actionCreator())
expectType<PayloadAction<number | undefined>>(actionCreator(undefined))

// typings:expect-error
expectType<PayloadAction<number>>(actionCreator())
Expand Down Expand Up @@ -104,9 +104,9 @@ function expectType<T>(p: T): T {
{ type: 'action' }
) as PayloadActionCreator<number>

const actionCreator2: ActionCreator<
PayloadAction<number>
> = payloadActionCreator2
const actionCreator2: ActionCreator<PayloadAction<
number
>> = payloadActionCreator2
}

/* createAction() */
Expand Down