Skip to content

Commit 6d3528a

Browse files
committed
Change default createAction payload type to void to make it required
1 parent 209bf06 commit 6d3528a

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

src/createAction.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { IsUnknownOrNonInferrable } from './tsHelpers'
1010
* @template M The type of the action's meta (optional)
1111
*/
1212
export type PayloadAction<
13-
P = any,
13+
P = void,
1414
T extends string = string,
1515
M = void
1616
> = WithOptionalMeta<M, WithPayload<P, Action<T>>>
@@ -62,7 +62,7 @@ export type ActionCreatorWithPayload<
6262
* An action creator that produces actions with a `payload` attribute.
6363
*/
6464
export type PayloadActionCreator<
65-
P = any,
65+
P = void,
6666
T extends string = string,
6767
PA extends PrepareAction<P> | void = void
6868
> = IfPrepareActionMethodProvided<
@@ -94,7 +94,7 @@ export type PayloadActionCreator<
9494
* If this is given, the resulting action creator will pass it's arguments to this method to calculate payload & meta.
9595
*/
9696

97-
export function createAction<P = any, T extends string = string>(
97+
export function createAction<P = void, T extends string = string>(
9898
type: T
9999
): PayloadActionCreator<P, T>
100100

type-tests/files/createAction.typetest.ts

+12-8
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,19 @@ function expectType<T>(p: T): T {
2222
* Test: PayloadAction type parameter is optional (defaults to `any`).
2323
*/
2424
{
25+
// typings:expect-error
2526
const action: PayloadAction = { type: '', payload: 5 }
27+
// typings:expect-error
2628
const numberPayload: number = action.payload
29+
// typings:expect-error
2730
const stringPayload: string = action.payload
2831
}
2932

3033
/*
3134
* Test: PayloadAction has a string type tag.
3235
*/
3336
{
34-
const action: PayloadAction = { type: '', payload: 5 }
37+
const action: PayloadAction<number> = { type: '', payload: 5 }
3538

3639
// typings:expect-error
3740
const action2: PayloadAction = { type: 1, payload: 5 }
@@ -41,7 +44,7 @@ function expectType<T>(p: T): T {
4144
* Test: PayloadAction is compatible with Action<string>
4245
*/
4346
{
44-
const action: PayloadAction = { type: '', payload: 5 }
47+
const action: PayloadAction<number> = { type: '', payload: 5 }
4548
const stringAction: Action<string> = action
4649
}
4750

@@ -58,10 +61,12 @@ function expectType<T>(p: T): T {
5861
payload
5962
}),
6063
{ type: 'action' }
61-
) as PayloadActionCreator
64+
) as PayloadActionCreator<number>
6265

6366
expectType<PayloadAction<number>>(actionCreator(1))
67+
// typings:expect-error
6468
expectType<PayloadAction<undefined>>(actionCreator())
69+
// typings:expect-error
6570
expectType<PayloadAction<undefined>>(actionCreator(undefined))
6671

6772
// typings:expect-error
@@ -110,22 +115,21 @@ function expectType<T>(p: T): T {
110115
}
111116

112117
/*
113-
* Test: createAction() type parameter is optional (defaults to `any`).
118+
* Test: createAction() type parameter is required, not inferred (defaults to `void`).
114119
*/
115120
{
116-
const increment = createAction('increment')
121+
const increment = createAction<number>('increment')
117122
const n: number = increment(1).payload
123+
// typings:expect-error
118124
const s: string = increment('1').payload
119-
120-
// but infers the payload type to be the argument type
121125
// typings:expect-error
122126
const t: string = increment(1).payload
123127
}
124128
/*
125129
* Test: createAction().type is a string literal.
126130
*/
127131
{
128-
const increment = createAction('increment')
132+
const increment = createAction<number, 'increment'>('increment')
129133
const n: string = increment(1).type
130134
const s: 'increment' = increment(1).type
131135

0 commit comments

Comments
 (0)