Describe middleware input and output mutators
Typing custom middleware
Zustand · Type Alias
StoreMutatorIdentifier is Zustand's type-level identifier for middleware mutators. It appears in mutator tuples that describe how middleware changes a store's type.
StoreMutatorIdentifier is the identifier type used in Zustand's higher-kinded mutator tuples. Middleware typings use it to describe the mutators a state creator receives or produces, while each identifier is paired with an associated type.
type StoreMutatorIdentifier = keyof StoreMutators<unknown, unknown>A type-level identifier used in [StoreMutatorIdentifier, unknown] mutator tuples.
import type { StoreMutatorIdentifier } from 'zustand'
type Logger = <
T,
Mps extends [StoreMutatorIdentifier, unknown][] = [],
Mcs extends [StoreMutatorIdentifier, unknown][] = [],
>(
f: StateCreator<T, Mps, Mcs>,
name?: string,
) => StateCreator<T, Mps, [['logger', never], ...Mcs]>This type models a middleware that adds a named mutator to a Zustand state creator.
Typing custom middleware
Store API augmentation
Middleware composition
StoreMutatorIdentifier is used as the first element of a mutator tuple, whose second element carries the mutator's associated type. StateCreator uses arrays of these tuples to represent incoming and outgoing middleware mutations.
Follow-up: Why is the second tuple element needed? It lets a middleware carry configuration or another type parameter through the mutator list, so the resulting store API can be augmented precisely.
A custom middleware typically constrains its mutator lists with [StoreMutatorIdentifier, unknown][]. It then prepends its own identifier and associated type to the outgoing list returned by StateCreator.
Follow-up: How is the runtime store augmentation connected to the type declaration? Module augmentation of StoreMutators maps the identifier to the corresponding store shape.
The identifier is part of Zustand's higher-kinded mutator system, which allows middleware to change the type of the store. This lets TypeScript expose middleware-added APIs while preserving generic state and middleware composition.
Follow-up: What should be checked when composing middleware? The input and output mutator lists must be ordered and parameterized consistently with each middleware's StateCreator signature.
StateCreatorMutateStoreApiStoreMutators