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: When a state listens to more than one event, strict event type checking breaks #4951

Open
rexsuit opened this issue Jun 22, 2024 · 1 comment

Comments

@rexsuit
Copy link

rexsuit commented Jun 22, 2024

Bug or feature request?

Bug with xstate v5.13.2

Description:

When creating a strictly typed machine with the setup function, and adding event definitions, TS informs if we try to listen to an undefined event.

image

However, this behavior breaks when we try to listen to more than one event on the same state

image

(Bug) Expected result:

I am able to listen to more than one event per state, and still have TS guarantee that I am used pre-defined events

(Bug) Actual result:

Listening to more than one event per state stops TS from informing me of my using undefined events

Link to reproduction or proof-of-concept:

Here's a codesandbox of both the machine where TS shows an where listens to an undefined event (and shows a TS error), and another machine the same sate listens to more than one event, and fails to inform the vscode user of the fact that an undefined error is being listened to.

Alternatively, here's a snipper that can be pasted into local dev. The snippet shoud cause a TS error if it listens to an event not defined in the "types" object, but does not as long as more than one "on" event is listened to.

export const testMachine = setup({
  types: {
    events: {} as { type: "start" },
  },
}).createMachine({
  initial: "Idle",
  states: {
    Idle: {
      on: {
        fakeEvent: "Pending",
        start: "Pending",
      },
    },
    Pending: {},
  },
});
@davidkpiano
Copy link
Member

davidkpiano commented Jun 22, 2024

From my quick investigation, this has to do with EventDescriptor<TEvent> here: https://github.com/statelyai/xstate/blob/main/packages/core/src/types.ts#L577

Changing this to just TEvent['type'] solves the problem, although we still have to account for partial event descriptors.

cc. @Andarist

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

No branches or pull requests

2 participants