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 bug: No Type Error for Non Declared Event #4718

Open
Pirororor opened this issue Jan 31, 2024 · 1 comment
Open

Bug: Typescript bug: No Type Error for Non Declared Event #4718

Pirororor opened this issue Jan 31, 2024 · 1 comment
Assignees
Labels

Comments

@Pirororor
Copy link

XState version

XState version 5

Description

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

import { setup, assign } from "xstate";

export const stateMachine = setup({
  types: {
    input: {} as {
      numTimesWalked: number;
      numTimesStanding?: number;
    },
    context: {} as {
      numTimesWalked: number;
      numTimesStanding?: number;
    },
    events: {} as
      | { type: "toggle" }
      | { type: "reset" }
      | { type: "turnOff" }
      | { type: "walk" }
      | { type: "stopWalking" }
      | { type: "startWalkingFromIdle" }
      | { type: "setNumTimesWalked"; num: number }
      | { type: "setNumTimesStanding"; num: number },
  },
}).createMachine({
  id: "StateMachineWithUpdateContextInput",
  description: "Simple Test Machine",
  initial: "idle",
  context: ({ input }) => ({
    numTimesWalked: input.numTimesWalked,
  }),
  // global events
  on: {
    reset: {
      target: ".idle",
      actions: assign(() => ({
        numTimesStanding: 0,
        numTimesWalked: 0,
      })),
    },
    turnOff: { target: ".off" },
  },
  // state related events
  states: {
    idle: {
      on: {
        toggle: "active",
        startWalkingFromIdle: "active.walking",
        setNumTimesWalked: {
          actions: assign(({ event: { num } }) => ({
            numTimesWalked: num,
          })),
        },
      },
    },
    active: {
      on: {
        toggle: "idle",
      },
      initial: "standing",
      states: {
        standing: {
          entry: assign(({ context }) => ({
            numTimesStanding: (context.numTimesStanding || 0) + 1,
          })),
          on: {
            eventThatDoesNotExist: {
              actions: assign(({ event: { num } }) => ({
                numTimesStanding: num,
              })),
            },
            walk: "walking",
          },
        },
        walking: {
          entry: assign(({ context }) => ({
            numTimesWalked: context.numTimesWalked + 1,
          })),
          on: {
            stopWalking: "standing",
          },
        },
      },
    },
    off: {
      type: "final",
      description: "This is the final state. No transitions can happen from me",
    },
  },
});

Expected result

Typescript error to be shown for "eventThatDoesNotExist" event type.

Actual result

Typescript error is not shown for "eventThatDoesNotExist" event type.

It wasnt until I changed 3 more events to something that does not exist, then the typescript error appears. Even then, the type error is only for 2 of the erronous events. (Refer to screenshot)

image

Reproduction

Can refer to the code snippet in the description

Additional context

No response

@Vithop
Copy link

Vithop commented Feb 13, 2024

Hey I'm having the same issue as well. Defining the types doesn't restrict me from adding erroneous events to my machine.

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

3 participants