Skip to content

Commit 980dcbe

Browse files
authored
simplify signatures of ActionCreatorWithOptionalPayload and `A… (#428)
1 parent 5929c30 commit 980dcbe

File tree

3 files changed

+10
-24
lines changed

3 files changed

+10
-24
lines changed

etc/redux-toolkit.api.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ export interface ActionCreatorWithNonInferrablePayload<T extends string = string
2828

2929
// @public
3030
export interface ActionCreatorWithOptionalPayload<P, T extends string = string> extends BaseActionCreator<P, T> {
31-
(payload?: undefined): PayloadAction<undefined, T>;
32-
<PT extends Diff<P, undefined>>(payload?: PT): PayloadAction<PT, T>;
31+
(payload?: P): PayloadAction<P, T>;
3332
}
3433

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

4039
// @public
4140
export interface ActionCreatorWithPayload<P, T extends string = string> extends BaseActionCreator<P, T> {
42-
<PT extends P>(payload: PT): PayloadAction<PT, T>;
4341
(payload: P): PayloadAction<P, T>;
4442
}
4543

src/createAction.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,12 @@ export interface ActionCreatorWithPreparedPayload<
123123
*/
124124
export interface ActionCreatorWithOptionalPayload<P, T extends string = string>
125125
extends BaseActionCreator<P, T> {
126-
/**
127-
* Calling this {@link redux#ActionCreator} without arguments will
128-
* return a {@link PayloadAction} of type `T` with a payload of `undefined`
129-
*/
130-
(payload?: undefined): PayloadAction<undefined, T>
131126
/**
132127
* Calling this {@link redux#ActionCreator} with an argument will
133-
* return a {@link PayloadAction} of type `T` with a payload of `P`
128+
* return a {@link PayloadAction} of type `T` with a payload of `P`.
129+
* Calling it without an argument will return a PayloadAction with a payload of `undefined`.
134130
*/
135-
<PT extends Diff<P, undefined>>(payload?: PT): PayloadAction<PT, T>
131+
(payload?: P): PayloadAction<P, T>
136132
}
137133

138134
/**
@@ -160,12 +156,6 @@ export interface ActionCreatorWithoutPayload<T extends string = string>
160156
*/
161157
export interface ActionCreatorWithPayload<P, T extends string = string>
162158
extends BaseActionCreator<P, T> {
163-
/**
164-
* Calling this {@link redux#ActionCreator} with an argument will
165-
* return a {@link PayloadAction} of type `T` with a payload of `P`
166-
* If possible, `P` will be narrowed down to the exact type of the payload argument.
167-
*/
168-
<PT extends P>(payload: PT): PayloadAction<PT, T>
169159
/**
170160
* Calling this {@link redux#ActionCreator} with an argument will
171161
* return a {@link PayloadAction} of type `T` with a payload of `P`
@@ -333,8 +323,6 @@ export function getType<T extends string>(
333323

334324
// helper types for more readable typings
335325

336-
type Diff<T, U> = T extends U ? never : T
337-
338326
type IfPrepareActionMethodProvided<
339327
PA extends PrepareAction<any> | void,
340328
True,

type-tests/files/createAction.typetest.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ function expectType<T>(p: T): T {
7373
{ type: 'action' }
7474
) as PayloadActionCreator<number | undefined>
7575

76-
expectType<PayloadAction<number>>(actionCreator(1))
77-
expectType<PayloadAction<undefined>>(actionCreator())
78-
expectType<PayloadAction<undefined>>(actionCreator(undefined))
76+
expectType<PayloadAction<number | undefined>>(actionCreator(1))
77+
expectType<PayloadAction<number | undefined>>(actionCreator())
78+
expectType<PayloadAction<number | undefined>>(actionCreator(undefined))
7979

8080
// typings:expect-error
8181
expectType<PayloadAction<number>>(actionCreator())
@@ -104,9 +104,9 @@ function expectType<T>(p: T): T {
104104
{ type: 'action' }
105105
) as PayloadActionCreator<number>
106106

107-
const actionCreator2: ActionCreator<
108-
PayloadAction<number>
109-
> = payloadActionCreator2
107+
const actionCreator2: ActionCreator<PayloadAction<
108+
number
109+
>> = payloadActionCreator2
110110
}
111111

112112
/* createAction() */

0 commit comments

Comments
 (0)