Skip to content

@ue-too/being / createStateMachineSchemaWithInferredStates

Function: createStateMachineSchemaWithInferredStates()

createStateMachineSchemaWithInferredStates<Context, EventPayloadMapping, States, EventOutputMapping>(schema): StateMachineSchema<Context, EventPayloadMapping, ExtractStateNames<States>, EventOutputMapping>

Defined in: schema-factory.ts:249

Helper function to create a typed state machine schema with inferred state names. Use this when you have a const states array and want TypeScript to infer the state names.

Type Parameters

Context

Context extends BaseContext

EventPayloadMapping

EventPayloadMapping

States

States extends readonly string[]

EventOutputMapping

EventOutputMapping extends Partial<Record<keyof EventPayloadMapping, unknown>> = DefaultOutputMapping<EventPayloadMapping>

Parameters

schema

Omit<StateMachineSchema<Context, EventPayloadMapping, ExtractStateNames<States>, EventOutputMapping>, "states"> & object

Returns

StateMachineSchema<Context, EventPayloadMapping, ExtractStateNames<States>, EventOutputMapping>

Example

typescript
const states = ["IDLE", "RUNNING", "PAUSED"] as const;
const schema = createStateMachineSchemaWithInferredStates<TimerContext, TimerEvents>({
  states,
  events: { start: {}, stop: {} },
  initialState: "IDLE",
  stateDefinitions: [
    {
      name: "IDLE", // TypeScript knows this must be one of the states
      transitions: [
        {
          event: "start",
          targetState: "RUNNING", // TypeScript knows this must be one of the states
        }
      ]
    }
  ]
});