-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
A semi-common mistake is to dispatch an action creator without calling it first, e.g. dispatch(actionCreator)
.
This doesn't currently raise any errors:
- In Typescript, RTK's action creators having a static
type
property mean they conform to the Action type. - In runtime, dispatching a function is usually picked up by the thunk middleware, which would end up returning
actionCreator(dispatch, getState, extra)
.
An error would only be raised with no thunk middleware.
From a Typescript perspective, this would likely only be solveable by adding some sort of [notAnAction)?: never
property to the base Action type, and adding a [notAnAction]: true
property to the action creators, making it incompatible. (this also wouldn't prevent action creators without parameters being valid thunks, unless something similar was done for ThunkAction)
From a runtime perspective, this could be fairly simple: a dev-only middleware similar to the serializability and immutable middlewares looks for a unique property we assign to RTK action creators, and warns in console if it sees one.