createFsm
Call Signature
Section titled “Call Signature”createFsm<
TVocab>():CurriedFsmFactory<VocabularyInputs<TVocab>,VocabularyEvents<TVocab>>
Defined in: fsm.ts:429
Create a single-client FSM from a config object.
Generic parameters are inferred automatically:
TCtxcomes fromconfig.context(defaults to{}if omitted).TStatesis captured withconstinference to preserve string literal types, enabling compile-time validation of transition targets andhandle()input names.
State names, input names, and all handler signatures derive from TStates.
A config-level onDispose hook (#194) runs from dispose() with the
context; see FsmConfig.onDispose for the contract.
To type each input’s payload as well (#195), call the curried form with an
input payload map: createFsm<TInputs>()({...}). The zero-argument call
fixes TInputs; the returned function infers everything else from the
config exactly as the direct form does. The split exists because TypeScript
has no partial type-argument inference — the map can’t ride along with the
inferred parameters. Under a map, handler payload parameters are inferred
from the map’s tuples and handle() enforces them at the call site. The
map is also the complete input vocabulary: handler keys outside it are
compile errors.
To type custom events as well (#183), put a vocabulary bag in the same
slot: createFsm<{ events: TEvents }>() for events alone, or
createFsm<{ inputs: TInputs; events: TEvents }>() for both. Under an
event map, emit() in handlers and on the instance is keyed by the map’s
names and checks each payload, and on() types the declared events
alongside the built-in ones.
Type Parameters
Section titled “Type Parameters”TVocab
Section titled “TVocab”TVocab extends object & { [K in string | number | symbol]: never } | InputMapLike<TVocab> = InputMap
Returns
Section titled “Returns”CurriedFsmFactory<VocabularyInputs<TVocab>, VocabularyEvents<TVocab>>
Examples
Section titled “Examples”Typed input payloads via the curried form
Typed custom events via a vocabulary bag
Call Signature
Section titled “Call Signature”createFsm<
TCtx,TStates,TStateNames,TBubbles>(config):Fsm<TCtx, keyofTStates&string,TBubbles|Exclude<{ [S in string | number | symbol]: keyof TStates[S] & string }[keyofTStates],SpecialStateKeys> | { [S in string | number | symbol]: TStates[S] extends { _child: C } ? InputNamesOfInstance<C> : never }[keyofTStates],TBubbles>
Defined in: fsm.ts:437
Create a single-client FSM from a config object.
Generic parameters are inferred automatically:
TCtxcomes fromconfig.context(defaults to{}if omitted).TStatesis captured withconstinference to preserve string literal types, enabling compile-time validation of transition targets andhandle()input names.
State names, input names, and all handler signatures derive from TStates.
A config-level onDispose hook (#194) runs from dispose() with the
context; see FsmConfig.onDispose for the contract.
To type each input’s payload as well (#195), call the curried form with an
input payload map: createFsm<TInputs>()({...}). The zero-argument call
fixes TInputs; the returned function infers everything else from the
config exactly as the direct form does. The split exists because TypeScript
has no partial type-argument inference — the map can’t ride along with the
inferred parameters. Under a map, handler payload parameters are inferred
from the map’s tuples and handle() enforces them at the call site. The
map is also the complete input vocabulary: handler keys outside it are
compile errors.
To type custom events as well (#183), put a vocabulary bag in the same
slot: createFsm<{ events: TEvents }>() for events alone, or
createFsm<{ inputs: TInputs; events: TEvents }>() for both. Under an
event map, emit() in handlers and on the instance is keyed by the map’s
names and checks each payload, and on() types the declared events
alongside the built-in ones.
Type Parameters
Section titled “Type Parameters”TCtx extends object = Record<string, never>
TStates
Section titled “TStates”TStates extends Record<string, Record<string, unknown>> = Record<string, Record<string, unknown>>
TStateNames
Section titled “TStateNames”TStateNames extends string = keyof TStates & string
TBubbles
Section titled “TBubbles”TBubbles extends string = never
Parameters
Section titled “Parameters”config
Section titled “config”FsmConfig<TCtx, TStates, TStateNames, TBubbles>
Returns
Section titled “Returns”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>
Examples
Section titled “Examples”Typed input payloads via the curried form
Typed custom events via a vocabulary bag