statelyai / xstate

Actor-based state management & orchestration for complex app logic.
https://stately.ai/docs
MIT License
27.24k stars 1.26k forks source link

Bug: [Typescript] event target doesn't get restricted to declared state #4865

Open BrandonTing opened 7 months ago

BrandonTing commented 7 months ago

XState version

XState version 5

Description

I created the following state machine. However, when I tried declaring a non-existant state Non exist state, no type error is generated. (Refer to code snippet)

import { setup } from "xstate";

const loginMachine = setup({
    types: {
        context: {} as { userId: string, name: string },
        events: {} as
            | { type: "Login" }
            | { type: "Log out" }
            | { type: "Test" },
    },
}).createMachine({
    context: {
        userId: "",
        name: "",
    },
    id: "Simple Login",
    initial: "Guest",
    states: {
        "Guest": {
            on: {
                "Test": {
                    // @ts-expect-error
                    target: "Non exist state"
                },
                Login: {
                    target: "Login Success"
                }
            },
        },
        "Login Success": {
            on: {
                "Log out": {
                    target: "Guest"
                }
            }
        },
    },
});

export default loginMachine

Expected result

Typescript error to be shown for target: "Non exist state"

Actual result

No type error shown

Reproduction

https://stackblitz.com/edit/github-4b6ck8

Additional context

No response

davidkpiano commented 7 months ago

This is currently as designed (i.e., not implemented yet).

BrandonTing commented 7 months ago

Hi David,

Thanks for clarification! I believe this will improve DX a ton. Is this feature on the roadmap? Really appreciated.

davidkpiano commented 7 months ago

No problem! That would be a question for @Andarist to see if this is even possible.