Skip to content

FsmConfig

Defined in: types.ts:343

Configuration object for creating an FSM.

// TCtx inferred as { tickCount: number }, TStates inferred from states object:
createFsm({
  id: "traffic-light",
  initialState: "green",         // validated against state keys
  context: { tickCount: 0 },     // inference site for TCtx
  states: {
    green:  { timeout: "yellow" }, // "yellow" validated against state keys
    yellow: { timeout: "red" },
    red:    { timeout: "green" },
  },
});

TCtx

The context type (Fsm) or client type (BehavioralFsm). For Fsm, this is inferred from the context property. For BehavioralFsm, it’s the client object type provided explicitly or as a generic parameter.

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

The literal states object type. Captured by the factory function’s generic parameter (ideally with const to preserve string literal types). Defaults to a loose record for unconstrained usage.

optional context: TCtx

Defined in: types.ts:370

Initial context data. The type is inferred from this value and flows into every handler’s ctx parameter.

For BehavioralFsm, this property is optional and serves only as a type constraint — the client object IS the context.


id: string

Defined in: types.ts:351

Unique identifier for this FSM


initialState: NoInfer<keyof TStates & string>

Defined in: types.ts:361

The state to start in. Must be a key of states.

Wrapped in NoInfer to prevent TypeScript from using this value as an inference site for TStates. Without it, initialState: "green" could narrow TStates to only have a “green” key. We want inference to come exclusively from the states property.


states: ValidateStates<TCtx, TStates>

Defined in: types.ts:373

State definitions. Keys become the state name union.