Function: composeGuards()
Documentation / @xmachines/play-xstate / composeGuards
function composeGuards<TContext, TEvent>(guards): ComposedGuard;Defined in: packages/play-xstate/src/guards/compose.ts:77
Compose guards with AND logic using XState’s and() helper
Combines multiple guard predicates using AND semantics—all guards must pass for
the composition to succeed. Uses XState’s built-in and() helper to ensure proper
type inference and machine serialization.
Architectural Context: Supports Actor Authority (INV-01) by enabling declarative guard composition in state machine transitions. Guards enforce business logic rules that determine whether navigation or actions are valid.
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
TContext | any | State machine context type |
TEvent | any | Event type |
Parameters
| Parameter | Type | Description |
|---|---|---|
guards | GuardArray<TContext, TEvent> | Array of guard predicates or guard names (string references) |
Returns
XState and() guard composition
Throws
If guards array is empty
Examples
AND composition with named guards
import { setup } from "xstate";import { composeGuards } from "@xmachines/play-xstate";
const machine = setup({ guards: { isLoggedIn: ({ context }) => !!context.userId, hasPermission: ({ context }) => context.permissions.includes("admin"), },}).createMachine({ on: { accessAdmin: { // Both guards must pass guard: composeGuards(["isLoggedIn", "hasPermission"]), target: "adminPanel", }, },});AND composition with inline predicates
import { composeGuards } from "@xmachines/play-xstate";
guard: composeGuards([({ context }) => context.age >= 18, ({ context }) => context.verified]);See
- composeGuardsOr for OR composition
- negateGuard for NOT logic