Skip to content

createFsm

createFsm<TCtx, TStates, TStateNames, TBubbles>(config): Fsm<TCtx, keyof TStates & string, TBubbles | Exclude<{ [S in string | number | symbol]: keyof TStates[S] & string }[keyof TStates], SpecialStateKeys> | { [S in string | number | symbol]: TStates[S] extends { _child: C } ? InputNamesOfInstance<C> : never }[keyof TStates], TBubbles>

Defined in: fsm.ts:241

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>>

TStateNames extends string = keyof TStates & string

TBubbles extends string = never

FsmConfig<TCtx, TStates, TStateNames, TBubbles>

Fsm<TCtx, keyof TStates & string, TBubbles | Exclude<{ [S in string | number | symbol]: keyof TStates[S] & string }[keyof TStates], SpecialStateKeys> | { [S in string | number | symbol]: TStates[S] extends { _child: C } ? InputNamesOfInstance<C> : never }[keyof TStates], TBubbles>

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