{"id":13537418,"url":"https://github.com/mattpocock/xstate-codegen","last_synced_at":"2025-04-02T04:30:42.345Z","repository":{"id":42249570,"uuid":"283796947","full_name":"mattpocock/xstate-codegen","owner":"mattpocock","description":"A codegen tool for 100% TS type-safety in XState","archived":true,"fork":false,"pushed_at":"2023-03-05T08:46:02.000Z","size":1637,"stargazers_count":245,"open_issues_count":38,"forks_count":12,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-02-15T02:19:19.708Z","etag":null,"topics":["cli","codegen","state-machines","typescript","xstate"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mattpocock.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null},"funding":{"github":["mattpocock"]}},"created_at":"2020-07-30T14:26:54.000Z","updated_at":"2024-07-13T05:26:56.000Z","dependencies_parsed_at":"2024-01-06T07:56:57.741Z","dependency_job_id":"efe5b1bf-2261-4ab7-8408-e7c41f483449","html_url":"https://github.com/mattpocock/xstate-codegen","commit_stats":{"total_commits":136,"total_committers":10,"mean_commits":13.6,"dds":"0.40441176470588236","last_synced_commit":"98186680d0bb3b96b6199321372bef22d654511c"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattpocock%2Fxstate-codegen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattpocock%2Fxstate-codegen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattpocock%2Fxstate-codegen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattpocock%2Fxstate-codegen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mattpocock","download_url":"https://codeload.github.com/mattpocock/xstate-codegen/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246756911,"owners_count":20828794,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cli","codegen","state-machines","typescript","xstate"],"created_at":"2024-08-01T09:00:58.848Z","updated_at":"2025-04-02T04:30:41.824Z","avatar_url":"https://github.com/mattpocock.png","language":"TypeScript","readme":"\u003e 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.\n\n\u003e UPDATE: We did it! Typegen is now part of XState core. See the [announcement blog post](https://stately.ai/blog/introducing-typescript-typegen-for-xstate) to learn more. Typegen should be used in **100%** of circumstances - it is an improved version of xstate-codegen.\n\n## Type Safe State Machines\n\n`xstate-codegen` gives you 100% type-safe usage of XState in Typescript. [Try it out at this codesandbox!](https://codesandbox.io/s/xstate-codegen-example-7etw2?file=/src/demo.machine.ts)\n\nYou get type safety on:\n\n- Transition targets: `on: {EVENT: 'deep.nested.state'}`\n- Services\n- Guards\n- Activities\n- Actions\n- The `initial` attribute\n- `state.matches('deep.nested.state')`\n\nThis 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.\n\n## Usage\n\n### CLI\n\n`xstate-codegen \"src/**/**.machine.ts\"`\n\n### Inside code\n\nInstead of importing `createMachine` or `Machine` from `xstate`, import them from `@xstate/compiled`:\n\n```ts\nimport { createMachine } from '@xstate/compiled';\n\nconst machine = createMachine();\n```\n\nYou must pass three type options to `createMachine/Machine`:\n\n1. The desired shape of your machine's context\n2. The list of events your machine accepts, typed in a discriminated union (`type Event = { type: 'GO' } | { type: 'STOP' };`)\n3. A string ID for your machine, unique to your project.\n\nFor instance:\n\n```ts\nimport { Machine } from '@xstate/compiled';\n\ninterface Context {}\n\ntype Event = { type: 'DUMMY_TYPE' };\n\nconst machine = Machine\u003cContext, Event, 'uniqueId'\u003e({});\n```\n\n### Usage with React\n\n```ts\nimport { useMachine } from '@xstate/compiled/react';\nimport { machine } from './myMachine.machine'\n\nconst [state, dispatch] = useMachine(machine, {\n  // all options in here will be type checked\n})\n```\n\n### Usage with Interpret\n\n```ts\nimport { interpret } from '@xstate/compiled';\nimport { machine } from './myMachine.machine'\n\nconst service = interpret(machine, {\n  // all options in here will be type checked\n})\n```\n\n## Options\n\n### Once\n\n`xstate-codegen \"src/**/**.machine.ts\" --once`\n\nBy default, the CLI watches for changes in your files. Running `--once` runs the CLI only once.\n\n### Out Dir\n\n`xstate-codegen \"src/**/**.machine.ts\" --outDir=\"src\"`\n\nBy 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.\n\n\u003e Note, this only writes the declaration files to the directory. The `.js` files still get written to `node_modules/@xstate/compiled`.\n","funding_links":["https://github.com/sponsors/mattpocock"],"categories":["Awesome XState","TypeScript"],"sub_categories":["Tools"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattpocock%2Fxstate-codegen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmattpocock%2Fxstate-codegen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattpocock%2Fxstate-codegen/lists"}