Skip to content

createFsm

createFsm<TCtx, TStates>(config): Fsm<TCtx, StateNamesOf<TStates>, InputNamesOf<TStates>>

Defined in: fsm.ts:217

Create a single-client FSM from a config object.

Generic parameters are inferred automatically:

  • TCtx comes from config.context (defaults to {} if omitted).
  • TStates is captured with const inference to preserve string literal types, enabling compile-time validation of transition targets and handle() input names.

State names, input names, and all handler signatures derive from TStates.

TCtx extends object = Record<string, never>

TStates extends Record<string, Record<string, unknown>> = Record<string, Record<string, unknown>>

FsmConfig<TCtx, TStates>

Fsm<TCtx, StateNamesOf<TStates>, InputNamesOf<TStates>>

const light = createFsm({
  id: "traffic-light",
  initialState: "green",
  context: { tickCount: 0 },
  states: {
    green:  { timeout: "yellow" },
    yellow: { timeout: "red" },
    red:    { timeout: "green" },
  },
});

light.handle("timeout"); // transitions green → yellow