Skip to content

Commit 90283e9

Browse files
committed
Ensure void action creators can't be passed as event handlers
1 parent 56ed8a4 commit 90283e9

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

packages/toolkit/src/createAction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export interface ActionCreatorWithoutPayload<T extends string = string>
144144
* Calling this {@link redux#ActionCreator} will
145145
* return a {@link PayloadAction} of type `T` with a payload of `undefined`
146146
*/
147-
(): PayloadAction<undefined, T>
147+
(noArgument: void): PayloadAction<undefined, T>
148148
}
149149

150150
/**

packages/toolkit/src/tests/createAction.typetest.ts renamed to packages/toolkit/src/tests/createAction.typetest.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import React from 'react'
12
import type { Action, AnyAction, ActionCreator } from 'redux'
23
import type {
34
PayloadAction,
@@ -344,3 +345,15 @@ import { expectType } from './helpers'
344345
type AnyPayload = ReturnType<typeof anyCreator>['payload']
345346
expectType<IsAny<AnyPayload, true, false>>(true)
346347
}
348+
349+
// Verify action creators should not be passed directly as arguments
350+
// to React event handlers if there shouldn't be a payload
351+
{
352+
const emptyAction = createAction<void>('empty/action')
353+
function TestComponent() {
354+
// This typically leads to an error like:
355+
// // A non-serializable value was detected in an action, in the path: `payload`.
356+
// @ts-expect-error Should error because `void` and `MouseEvent` aren't compatible
357+
return <button onClick={emptyAction}>+</button>
358+
}
359+
}

0 commit comments

Comments
 (0)