Skip to content

Function: composeGuardsOr()

Documentation / @xmachines/play-xstate / composeGuardsOr

function composeGuardsOr<TContext, TEvent>(guards): ComposedGuard;

Defined in: packages/play-xstate/src/guards/compose.ts:143

Compose guards with OR logic using XState’s or() helper

Combines multiple guard predicates using OR semantics—at least one guard must pass for the composition to succeed. Uses XState’s built-in or() helper for proper type inference.

Type Parameters

Type ParameterDefault typeDescription
TContextanyState machine context type
TEventanyEvent type

Parameters

ParameterTypeDescription
guardsGuardArray<TContext, TEvent>Array of guard predicates or guard names

Returns

ComposedGuard

XState or() guard composition

Throws

If guards array is empty

Example

OR composition with named guards

import { setup } from "xstate";
import { composeGuardsOr } from "@xmachines/play-xstate";
const machine = setup({
guards: {
isOwner: ({ context }) => context.role === "owner",
isAdmin: ({ context }) => context.role === "admin",
},
}).createMachine({
on: {
deleteResource: {
// Either guard can pass
guard: composeGuardsOr(["isOwner", "isAdmin"]),
actions: "delete",
},
},
});

See