Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
BrandonTing opened this issue Apr 24, 2024 · 3 comments
Open
Labels

Comments

@BrandonTing
Copy link

BrandonTing commented Apr 24, 2024

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
Copy link
Member

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

@BrandonTing
Copy link
Author

Hi David,

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

@davidkpiano
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants