Skip to content

HandlerDef

HandlerDef<TCtx, TStateNames> = TStateNames | HandlerFn<TCtx, TStateNames> | MachinaInstance

Defined in: types.ts:258

The union of valid handler definition forms for a state input.

  • TStateNames — string shorthand, auto-transitions to that state
  • HandlerFn — function that returns a state name (transition) or void (stay)
  • MachinaInstance — included to satisfy TypeScript’s structural widening when ValidateStates falls back to its constraint type. The per-key restriction on _child is still enforced by ValidateStates; this union member just prevents the inference engine from rejecting _child: childFsm before the conditional mapping can evaluate it.

TCtx

TStateNames extends string = string

states: {
  green: {
    timeout: "yellow",                    // string shorthand
    tick({ ctx }) { ctx.tickCount++; },   // function, no transition
    emergency({ ctx }) {                  // function, conditional transition
      if (ctx.severity > 5) return "red";
    },
  },
}