Skip to content

Function: negateGuard()

Documentation / @xmachines/play-xstate / negateGuard

function negateGuard<TContext, TEvent>(guard): ComposedGuard;

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

Negate a guard using XState’s not() helper

Inverts a guard’s result—if the guard passes, NOT fails; if guard fails, NOT passes. Uses XState’s built-in not() helper for proper serialization.

Type Parameters

Type ParameterDefault typeDescription
TContextanyState machine context type
TEventanyEvent type

Parameters

ParameterTypeDescription
guardstring | Guard<TContext, TEvent>Guard predicate or guard name to negate

Returns

ComposedGuard

XState not() guard negation

Example

NOT composition with named guard

import { setup } from "xstate";
import { negateGuard } from "@xmachines/play-xstate";
const machine = setup({
guards: {
isGuest: ({ context }) => !context.userId,
},
}).createMachine({
on: {
accessDashboard: {
// Allow if NOT a guest (i.e., authenticated)
guard: negateGuard("isGuest"),
target: "dashboard",
},
},
});

See