Combine multiple conditions in addMatcher
A reducer builder needs one predicate for a value that must satisfy several action-related conditions.
Redux Toolkit · Function
isAllOf creates a type-safe matcher that succeeds only when every supplied action creator or type guard matches the same value. The returned matcher can be used in reducers, middleware, or other TypeScript code.
isAllOf accepts one or more action creators or type guards and returns a new type guard. The returned function succeeds only when every supplied matcher accepts the same value, while preserving TypeScript narrowing for the matched result.
isAllOf(...matchers)| Name | Type | Description |
|---|---|---|
matchers | ActionCreator | TypeGuard | CustomActionCreator | One or more Redux Toolkit action creators, type guard functions, or custom action creators whose .match property is a type guard. |
A type guard function that returns true when all supplied matcher functions match the input.
import { createAction, isAllOf } from '@reduxjs/toolkit'
const saved = createAction<{ id: string }>('items/saved')
const synced = createAction<{ id: string }>('items/synced')
const isSavedAndSynced = isAllOf(saved, synced)
console.log(isSavedAndSynced(saved({ id: 'a' })))
// false: one action cannot match both action creators
This matcher is intentionally false for distinct action creators; use it when multiple supplied type guards can all describe the same value.
A reducer builder needs one predicate for a value that must satisfy several action-related conditions.
Middleware needs to recognize an action only when every supplied type guard agrees.
Application code needs a reusable predicate that both checks and narrows an action value.
isAllOf is a higher-order matcher: it receives one or more action creators or type guards and returns a type guard. That returned guard is true only when all supplied matchers match the input.
Follow-up: How would you match an action satisfying at least one condition? Use isAnyOf instead, whose returned matcher succeeds when any supplied matcher matches.
Its inputs can include Redux Toolkit action creators from createAction, createSlice, or createAsyncThunk, plus type guards and custom action creators with a .match type guard. This lets the same matcher work across standard and custom action definitions.
Follow-up: Why can a custom action creator participate? Because exposing .match as a type guard gives the matcher a supported way to test the action and narrow its type.
The returned function is also usable as a TypeScript type guard outside reducer matcher callbacks. That makes it useful wherever code needs to test and narrow a value against several action conditions.
Follow-up: Where are these utilities commonly used? They are especially useful with builder.addMatcher in createSlice or createReducer and in custom middleware.
isAnyOfcreateActioncreateSlicecreateAsyncThunkbuilder.addMatchercreateReducercustom middleware