Skip to content

Dev check middleware to warn about action creators being dispatched  #3413

@EskiMojo14

Description

@EskiMojo14

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions