Skip to content

HandlerDef

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

Defined in: types.ts:364

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 — NOT a handler you’d write under an input key. It’s here because ValidateStates types every state as the SAME Record value type, which combines named properties (_onEnter, _onExit, _child, "*") with a [input: string]: HandlerDef<...> index signature covering everything else. TypeScript requires every named property’s type to be assignable to the index signature’s type, so _child?: MachinaInstance only type-checks if HandlerDef itself includes MachinaInstance in its union. Remove this member and _child: someChildFsm stops compiling.

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";
    },
  },
}