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] Unknown action when using enqueue() and referencing an action defined with enqueueActions #4820

Open
offirgolan opened this issue Apr 1, 2024 · 4 comments
Assignees
Labels

Comments

@offirgolan
Copy link

offirgolan commented Apr 1, 2024

XState version

XState version 5

Description

Typescript errors when calling an action via enqueue when the action being called is defined with enqueueActions.

setup({
  actions: {
    foo: enqueueActions(({ enqueue }) => {
      enqueue({ type: "bar" }); // TS ERROR
    }),
    bar: enqueueActions(({ enqueue }) => {
      enqueue.raise({ type: "submit" });
    }),
  },
})

Expected result

Typescript should not error when calling enqueue({ type: "bar" });

Actual result

Object literal may only specify known properties, and 'type' does not exist in type

Reproduction

https://codesandbox.io/p/devbox/currying-darkness-nvv5vh

Additional context

No response

@offirgolan offirgolan added the bug label Apr 1, 2024
@Andarist
Copy link
Member

Andarist commented Apr 1, 2024

This is a current limitation that we are facing. TS can't infer this situation correctly: TS playground

@boneskull
Copy link

Can a type assertion be used as a workaround? I'm not sure what it'd be.

@offirgolan
Copy link
Author

@Andarist I found another TS error: TS Playground

setup({
  actions: {
    bar: enqueueActions(({ check }) => {
      check(stateIn('a'));
    }),
  },
}).createMachine({
  states: {
    a: {},
    b: {}
  }
});

+1 to @boneskull, if there is a TS limitation are there utility types that can be exposed to help mitigate the issue via type assertions?

@offirgolan
Copy link
Author

offirgolan commented Apr 5, 2024

FWIW Using createMachine with the deprecated second argument instead of setup and defining types.actions and types.guards works.

Because of these TS limitations in setup, should I switch back to using createMachine?

Edit:

Another alternative is to add types.actions and types.guards to setup.

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