XState Codegen is deprecated - we'll be bringing this exploratory work into XState core. We're convinced that typegen is the future of good typing in XState, and syncing it with core (and the XState VSCode extension) is the future.
UPDATE: We did it! Typegen is now part of XState core. See the announcement blog post to learn more. Typegen should be used in 100% of circumstances - it is an improved version of xstate-codegen.
xstate-codegen
gives you 100% type-safe usage of XState in Typescript. Try it out at this codesandbox!
You get type safety on:
on: {EVENT: 'deep.nested.state'}
initial
attributestate.matches('deep.nested.state')
This works by introspecting your machine in situ in your code. With this Thanos-level power, we can click our fingers and give you 100% type safety in your state machines.
xstate-codegen "src/**/**.machine.ts"
Instead of importing createMachine
or Machine
from xstate
, import them from @xstate/compiled
:
import { createMachine } from '@xstate/compiled';
const machine = createMachine();
You must pass three type options to createMachine/Machine
:
type Event = { type: 'GO' } | { type: 'STOP' };
)For instance:
import { Machine } from '@xstate/compiled';
interface Context {}
type Event = { type: 'DUMMY_TYPE' };
const machine = Machine<Context, Event, 'uniqueId'>({});
import { useMachine } from '@xstate/compiled/react';
import { machine } from './myMachine.machine'
const [state, dispatch] = useMachine(machine, {
// all options in here will be type checked
})
import { interpret } from '@xstate/compiled';
import { machine } from './myMachine.machine'
const service = interpret(machine, {
// all options in here will be type checked
})
xstate-codegen "src/**/**.machine.ts" --once
By default, the CLI watches for changes in your files. Running --once
runs the CLI only once.
xstate-codegen "src/**/**.machine.ts" --outDir="src"
By default, the CLI adds the required declaration files inside node_modules at node_modules/@xstate/compiled
. This writes the declaration files to a specified directory.
Note, this only writes the declaration files to the directory. The
.js
files still get written tonode_modules/@xstate/compiled
.