Skip to content

Commit

Permalink
simplify signatures of ActionCreatorWithOptionalPayload and `Action…
Browse files Browse the repository at this point in the history
…CreatorWithPayload`
  • Loading branch information
phryneas committed Mar 11, 2020
1 parent 36b25fc commit 982da2c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 24 deletions.
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

0 comments on commit 982da2c

Please sign in to comment.