Design, implement, review, and migrate XState v5 state machines and statecharts in TypeScript using modern v5 patterns. Use this whenever the user mentions XState, actors, state machines, statecharts, guards, transitions, workflows, or Stately, or is modeling non-trivial UI/app/process logic in a codebase that uses XState. Prefer a short machine sketch before code when requirements are fuzzy. If the problem is too simple for a state machine, say so and recommend @xstate/store instead.
91%
Does it follow best practices?
Run evals on this skill
Adds up to 20 points to the overall score
Passed
No findings from the security scan
Use this file when you encounter older XState code and need a fast translation path without turning the task into a full migration project.
Default to the smallest safe translation. Preserve local structure unless the user asked for broader normalization.
cond -> guardschema -> typesservices -> actorsinterpret(machine) -> createActor(machine)Prefer destructured arguments:
guard: ({ context, event }) => context.count > 0instead of older positional signatures:
cond: (context, event) => context.count > 0setup(...)For new code or explicit normalization, prefer:
const machine = setup({
actions: {
save: () => {
// ...
}
},
guards: {
isValid: ({ context }) => context.ready
},
actors: {
loadUser: fromPromise(async () => {
// ...
})
}
}).createMachine({
// ...
});This gives a single place for named actions, guards, actors, and delays.
For small migration tasks, you do not need to introduce setup(...) if a more local v5 translation is clearer and safer.
Prefer explicit transition objects:
on: {
submit: {
target: 'saving',
actions: [{ type: 'trackSubmit' }]
}
}If you are only fixing a local issue in an older file, preserve nearby style unless the user asked for migration.
In v5, a transition declared on a parallel region's root can re-enter that source region when either:
reenter: trueRe-entering the source region resets it to its initial substate and cancels active invokes inside that region. v4 was more forgiving here, so this can become a silent migration bug: the target region changes as expected, but the source region snaps back.
Smell:
const machine = setup({}).createMachine({
id: 'sync',
type: 'parallel',
states: {
editor: {
initial: 'idle',
states: {
idle: {},
dirty: {
invoke: { src: 'autosaveDraft' }
}
},
on: {
// Source is the editor region root; target is a sibling region.
sync: { target: '#sync.network.syncing' }
}
},
network: {
initial: 'offline',
states: {
offline: {},
syncing: {},
online: {}
}
}
}
});Better: declare the transition on the region that owns the target, so only that region changes:
network: {
initial: 'offline',
states: {
offline: {
on: {
sync: { target: 'syncing' }
}
},
syncing: {},
online: {
on: {
sync: { target: 'syncing' }
}
}
}
}For a self-reentering transition, do not put reenter: true on the region root unless resetting the whole region is intended.
// Bad: re-enters the editor region root and resets it.
editor: {
initial: 'idle',
states: {
idle: {},
debouncing: {
after: { 500: 'idle' }
}
},
on: {
input: { target: '.debouncing', reenter: true }
}
}Scope the re-entry to the substate whose lifecycle should restart:
editor: {
initial: 'idle',
states: {
idle: {
on: {
input: { target: 'debouncing' }
}
},
debouncing: {
after: { 500: 'idle' },
on: {
input: { target: 'debouncing', reenter: true }
}
}
}
}If several substates need the same event, declare the handler on those substates instead of hoisting it to the region root by default.
v4 code often uses older service patterns. In v5, prefer named actors:
invoke: {
src: 'loadUser'
}with implementations defined in setup({ actors: ... }).
Prefer:
const actor = createActor(machine);
actor.start();instead of:
const service = interpret(machine).start();Prefer types over schema:
types: {} as {
context: { count: number };
events: { type: 'increment' } | { type: 'reset' };
}Prefer object forms when the behavior is named or parameterized:
guard: { type: 'hasPermission', params: { role: 'admin' } }
actions: [{ type: 'track', params: { eventName: 'form.submitted' } }]Do not force a full v4 -> v5 rewrite when the user asked for a small bug fix or local migration. Translate only the touched area unless: