Skip to content

Commit 735a7ab

Browse files
author
Justin Krueger
committed
Fix PayloadAction inference
By wrapping the match in a single-element tuple, it prevents the conditional from being checked distributively, which otherwise has the effect of converting a union into an intersection when used with contra-variant types.
1 parent 3c40c56 commit 735a7ab

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

src/createSlice.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { createSliceSelector, createSelectorName } from './sliceSelector'
66
/**
77
* An action creator atttached to a slice.
88
*/
9-
export type SliceActionCreator<P> = P extends void
9+
export type SliceActionCreator<P> = [P] extends [void]
1010
? () => PayloadAction<void>
1111
: (payload: P) => PayloadAction<P>
1212

type-tests/files/createSlice.typetest.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,16 @@ import {
6060
reducers: {
6161
increment: state => state + 1,
6262
decrement: state => state - 1,
63-
multiply: (state, action: PayloadAction<number>) => state * action.payload
63+
multiply: (state, { payload }: PayloadAction<number | number[]>) =>
64+
Array.isArray(payload)
65+
? payload.reduce((acc, val) => acc * val, state)
66+
: state * payload
6467
}
6568
})
6669

6770
counter.actions.increment()
6871
counter.actions.multiply(2)
72+
counter.actions.multiply([2, 3, 4])
6973

7074
// typings:expect-error
7175
counter.actions.multiply()

0 commit comments

Comments
 (0)