Broaden middleware detection
An application uses action creators with a static type property but without the standard match method.
Redux Toolkit · Function
isActionCreator is a configurable predicate used by Redux Toolkit's action-creator invariant middleware. It identifies values that should be treated as action creators, including custom action-creator shapes.
isActionCreator is a configurable type-guard predicate for action-creator invariant middleware. It receives an unknown value and returns whether that value should be treated as an action creator; the middleware can then warn when the creator itself is dispatched instead of its returned action.
isActionCreator?: (action: unknown) => action is Function & { type?: unknown }| Name | Type | Description |
|---|---|---|
action | unknown | The value to classify as an action creator; its runtime type is unknown. |
A boolean type predicate: action is Function & { type?: unknown } in the documented option signature.
type and match members.type property.createActionCreatorInvariantMiddleware.getDefaultMiddleware rather than constructing it directly.dispatch; dispatching the function itself is the mistake this middleware is designed to detect.import { configureStore, createActionCreatorInvariantMiddleware } from '@reduxjs/toolkit'
import reducer from './reducer'
const isActionCreator = (
action: unknown,
): action is Function & { type: unknown } =>
typeof action === 'function' && 'type' in action
const middleware = createActionCreatorInvariantMiddleware({ isActionCreator })
const store = configureStore({
reducer,
middleware: () => [middleware],
})
export default storeThis custom predicate treats any function with a static type property as an action creator, allowing the invariant middleware to detect when such a function is dispatched without being called.
An application uses action creators with a static type property but without the standard match method.
A project defines a custom action-creator convention that the default predicate does not recognize.
A team wants warnings when a creator function is dispatched directly instead of being invoked.
isActionCreator?isActionCreator is a predicate used by the action-creator invariant middleware to recognize values that represent action creators. Its return type is a TypeScript type predicate, narrowing a matching value to a function with an optional type property.
Follow-up: Why does the default recognize both type and match? Redux Toolkit action creators expose a static type string and a match() method, so checking both identifies the standard action-creator shape.
The function receives an unknown value and should return a boolean. The documented custom example identifies values that are functions and have a type property, returning true for those values.
Follow-up: How would you include creators without a type property? Change the predicate to test the actual distinguishing characteristic of those creators, while preserving a sound type predicate.
Pass the predicate through the isActionCreator option when creating action-creator invariant middleware. This is useful when the default recognition rules do not cover an application's action-creator conventions.
Follow-up: Is explicit middleware creation normally required? Usually not: getDefaultMiddleware already includes the action-creator check middleware.
createActionCreatorInvariantMiddlewaregetDefaultMiddlewarecreateActioncreateSlice