defineStates
defineStates<
Config>(config:Config):StateMatchboxFactory<Config>
Defined in: define-states.ts:41
defineStates creates a type-safe state factory for your state machine.
Each key in the config becomes a state constructor, inferring parameters and data shape.
Usage:
const states = defineStates({ SomeEmptyState: undefined, // No parameters SomeValueState: "any value here" || 42, // Any value as data SomeStateWithCreate: (...anyParameters) => ({ ...anyPayload }),});// Usagestates.SomeEmptyState().key; // "SomeEmptyState"states.SomeValueState().data; // "any value here" || 42states.SomeStateWithCreate("param1", "param2").data; // { ...anyPayload }Type benefits:
- State keys and data are fully inferred
- Pattern matching and type guards are available
- Exhaustive match on state keys
Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
Config extends TaggedTypes |
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
config | Config |
Returns
Section titled “Returns”StateMatchboxFactory<Config>
Example
Section titled “Example” const states = defineStates({ Idle: undefined, Loading: (query: string) => ({ query }), Success: (query: string, results: string[]) => ({ query, results }), Error: (query: string, message: string) => ({ query, message }), });
// Usage: states.Idle().key // "Idle" states.Loading("search").data // { query: "search" } states.Success("search", ["a", "b"]).data // { query, results }