• This repository has been archived on 05/Mar/2023
  • Stars
    star
    244
  • Rank 165,885 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A codegen tool for 100% TS type-safety in XState

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.

Type Safe State Machines

xstate-codegen gives you 100% type-safe usage of XState in Typescript. Try it out at this codesandbox!

You get type safety on:

  • Transition targets: on: {EVENT: 'deep.nested.state'}
  • Services
  • Guards
  • Activities
  • Actions
  • The initial attribute
  • state.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.

Usage

CLI

xstate-codegen "src/**/**.machine.ts"

Inside code

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:

  1. The desired shape of your machine's context
  2. The list of events your machine accepts, typed in a discriminated union (type Event = { type: 'GO' } | { type: 'STOP' };)
  3. A string ID for your machine, unique to your project.

For instance:

import { Machine } from '@xstate/compiled';

interface Context {}

type Event = { type: 'DUMMY_TYPE' };

const machine = Machine<Context, Event, 'uniqueId'>({});

Usage with React

import { useMachine } from '@xstate/compiled/react';
import { machine } from './myMachine.machine'

const [state, dispatch] = useMachine(machine, {
  // all options in here will be type checked
})

Usage with Interpret

import { interpret } from '@xstate/compiled';
import { machine } from './myMachine.machine'

const service = interpret(machine, {
  // all options in here will be type checked
})

Options

Once

xstate-codegen "src/**/**.machine.ts" --once

By default, the CLI watches for changes in your files. Running --once runs the CLI only once.

Out Dir

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 to node_modules/@xstate/compiled.

More Repositories

1

ts-reset

A 'CSS reset' for TypeScript, improving types for common JavaScript API's
TypeScript
7,817
star
2

ts-error-translator

VSCode extension to turn TypeScript errors into plain English
TypeScript
2,360
star
3

xstate-catalogue

Professionally designed, interactive state machines
TypeScript
813
star
4

zod-fetch

Simple function for building a type-safe fetcher with Zod
TypeScript
664
star
5

total-typescript-monorepo

The home of all Matt's internal tooling
TypeScript
196
star
6

sextant

A development tool to chart application flows, then implement them in code
TypeScript
168
star
7

pkg-demo

TypeScript
166
star
8

xstate-next-boilerplate

A performant, robust starting point for any React application
TypeScript
130
star
9

graph-docs-cli

Revolutionise your docs using knowledge graphs built from Markdown
TypeScript
95
star
10

redux-xstate-poc

Manage your Redux side effects with XState. Use 100% of XState's features.
TypeScript
89
star
11

package-tools

76
star
12

typescript-node

TypeScript
55
star
13

esbuild-node

TypeScript
44
star
14

boilersuit

A super-powered generator for selectors, reducers, actions, constants and sagas in react-boilerplate
JavaScript
28
star
15

turborepo-stately-demo

JavaScript
27
star
16

tsconfig-creator

TypeScript
27
star
17

use-fsm-reducer

useReducer for state machine enthusiasts
TypeScript
26
star
18

make-route-map

Type-safe routing for single-page-applications
TypeScript
24
star
19

xstate-sync-to-context

TypeScript
24
star
20

xstate-test-playwright

TypeScript
22
star
21

total-typescript-monorepo-template

TypeScript
21
star
22

matt-cli

TypeScript
20
star
23

debates-stream

TypeScript
20
star
24

tt-package-demo

TypeScript
20
star
25

workshops

TypeScript
14
star
26

xstate-real-world-app

TypeScript
13
star
27

figma-xstate-plugin

TypeScript
11
star
28

xstate-stream-demo-app

A full-stack app built with Next.js, XState, Typescript and GraphQL.
TypeScript
11
star
29

practice-cli

JavaScript
8
star
30

my-monorepo

JavaScript
7
star
31

react-finland-talk

TypeScript
7
star
32

total-typescript-cli

TypeScript
7
star
33

create-t3-turbo-test

TypeScript
6
star
34

neverthrow-tutorial

TypeScript
6
star
35

pro-essentials-cut-down-to-chapters

TypeScript
6
star
36

xstate-sandpack-tutorials

TypeScript
5
star
37

total-typescript-helpers

TypeScript
5
star
38

as-prop-perf-demo

TypeScript
5
star
39

react-day-berlin-react-types

TypeScript
5
star
40

mattpocock

5
star
41

full-stack-ui

TypeScript
5
star
42

xstate-command-palette

TypeScript
4
star
43

matt-product-boilerplate

TypeScript
4
star
44

graphql-rest-contracts

Use graphql as a schema language to define a REST API
JavaScript
4
star
45

pro-essentials-cut-down

TypeScript
4
star
46

vscode-day-talk

TypeScript
3
star
47

project-references-demo

TypeScript
3
star
48

react-typescript-tutorial-05

TypeScript
3
star
49

video-ideas

TypeScript
3
star
50

gql-tada-testing

TypeScript
3
star
51

obs-stinger

TypeScript
2
star
52

voicehacker

JavaScript
2
star
53

wordpress-react-simplified

The simplest imaginable implementation of React with Wordpress. No external dependencies, just the PHP passing the page ID to the javascript, which goes and gets the correct page info and renders it.
JavaScript
2
star
54

cypress-xstate-demo

TypeScript
2
star
55

cypress-job-search

A simple web scraper built with Cypress and json-server
JavaScript
1
star
56

xstate-form-test

TypeScript
1
star
57

kelvin-chat

TypeScript
1
star
58

vscode-xstate

1
star
59

multi-step-form

TypeScript
1
star
60

xstate-inspect-storybook-example

JavaScript
1
star
61

react-typescript-boilerplate

JavaScript
1
star
62

turbo-ignore-demo

JavaScript
1
star