Skip to content

API Reference

Runtime types

All public types are exported from the package root:

ts
import type {
  // Core config
  MachineConfig,
  StateConfig,
  TransitionConfig,
  SubMachineConfig,

  // Functions
  Guard,
  Action,

  // Events
  EventObject,

  // Runtime
  MachineInstance,
  UseMachineOptions,
  TransitionRecord,
  MachineSnapshot,
  TransitionResult,

  // Wizard
  WizardStep,
  WizardOptions,
  WizardInstance,

  // Store
  MachineStoreAPI,

  // Utility
  Ctx,
} from '@macrulez/vue-state-machine'

TransitionResult also carries contextPatch: Partial<TContext> — the actual delta an action-driven transition applied (distinct from nextContext, the full resulting context). Used internally to merge parallel-region context changes without one region clobbering another's untouched fields — see Parallel States.

Generic inference

TypeScript infers TState, TEvent, and TContext from the config you pass to defineMachine. You rarely need to annotate them explicitly:

ts
const machine = defineMachine({
  id: 'traffic',
  initial: 'red', // TS infers TState = 'red' | 'green' | 'yellow'
  states: {
    red: { on: { NEXT: { target: 'green' } } }, // TEvent = 'NEXT'
    green: { on: { NEXT: { target: 'yellow' } } },
    yellow: { on: { NEXT: { target: 'red' } } },
  },
})

const { state } = useMachine(machine)
// state: Ref<'red' | 'green' | 'yellow'>
// send accepts only 'NEXT' — other strings are compile errors

For complex cases you can annotate explicitly:

ts
const machine = defineMachine<
  'idle' | 'loading' | 'error' | 'success',
  'SUBMIT' | 'SUCCESS' | 'FAILURE' | 'RETRY',
  { attempts: number; error: string | null }
>({ ... })