TypeScript-first state machines with type-safe pattern matching.
npm install matchinaMatchina lets you model states as tagged unions and drive transitions with full type inference. Pattern matching is exhaustive — TypeScript won't let you forget a state.
import { matchina, defineStates } from "matchina";
const states = defineStates({
Idle: undefined,
Playing: (trackId: string) => ({ trackId }),
Paused: (trackId: string) => ({ trackId }),
Stopped: undefined,
});
const player = matchina(
states,
{
Idle: { start: "Playing" },
Playing: { pause: "Paused", stop: "Stopped" },
Paused: { resume: "Playing", stop: "Stopped" },
Stopped: { start: "Playing" },
},
"Idle"
);
player.start("song-123");
const message = player.getState().match({
Playing: ({ trackId }) => `Now playing: ${trackId}`,
Paused: ({ trackId }) => `Paused: ${trackId}`,
Idle: () => "Ready",
Stopped: () => "Stopped",
});- Quickstart
- State Machines
- Tagged Unions (Matchbox)
- Promise Machines
- Lifecycle Hooks
- Hierarchical Machines
- React Integration
- Examples
MIT