Skip to content

Bug: any string can be passed to initial or target fields #5332

@tomdohnal

Description

@tomdohnal

XState version

XState version 5

Description

I'd expect to get TS errors for this code:

const feedbackMachine = setup({
  types: {
    context: {} as { feedback: string },
    events: {} as { type: "feedback.good" } | { type: "feedback.bad" },
  },
}).createMachine({
  context: {
    feedback: "",
  },
  initial: "NON_EXISTING_STATE", // <---- i'd expect a TS error here
  states: {
    idle: {
      on: {
        "feedback.good": {
          target: "NON_EXISTING_STATE", // <---- i'd expect a TS error here
        },
      },
    },
  },
});

Expected result

I should only be allowed to pass valid state values (in my example only idle) to the initial and target fields

Actual result

I'm allowed to pass a non existing state value to the initial and target fields, which then yields runtime errors

Reproduction

https://codesandbox.io/p/devbox/great-leavitt-fw32ld?file=%2Fsrc%2FfeedbackMachine.ts%3A10%2C69

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions