question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[4.10: TypeScript] Typed actions throw type errors

See original GitHub issue

Description Hello everybody, I just upgraded to version 4.10 ready to fix my send types. I’m so happy about this release! Thank you so much for introducing the third generic toSendEvent. I oftentimes had to go for <any, any> as a workaround and am so glad to finally change those. 🖤

When defining an assign action within the configuration object, TypeScript goes nuts.

actions: {
  assignCard: assign<MContext, SubmitEvent>({
    card: (_, event) => event.data.name
  })
}

I got the same error when passing the action to machine.withConfig().

Expected Result No type error should be thrown.

Actual Result TypeScript error pyramid of death 😬

image

Reproduction https://codesandbox.io/s/xstate-bug-typed-assign-action-in-configuration-object-5pc63?file=/src/index.tsx:1858-1981

Additional context

@xstate/react: 0.8.1,
xstate: 4.10.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:14 (12 by maintainers)

github_iconTop GitHub Comments

5reactions
danielkczcommented, May 22, 2020

Or a different workaround, imo slightly nicer 😃

actions: {
  assignCard: assign<MContext, SubmitEvent>({
    card: (_, event) => event.data.name
  }) as any // <-- this
}

I assume this will get better in V5?

4reactions
davidkpianocommented, May 21, 2020

Workaround:

    actions: {
      assignCard: assign<MContext, any>({
        card: (_, event) => (event as SubmitEvent).data.name
      })
    }
Read more comments on GitHub >

github_iconTop Results From Across the Web

[4.10: TypeScript] Typed actions throw type errors · Issue #1198
When defining an assign action within the configuration object, TypeScript goes nuts. ... I got the same error when passing the action to...
Read more >
TypeScript error when using Remix action inferred types
My original code used actionData?.errors?.[input]?._errors[0] in the hasErrors() function, but it threw a cannot index errors with type string ...
Read more >
Improving TypeScript error handling with exhaustive type ...
Discover an improved method for handling errors in TypeScript that solves problems that arise from returning null and throwing try...catch.
Read more >
Type-Safe Error Handling In TypeScript - DEV Community ‍ ‍
In this post, I introduce the concept of a Result type, which eliminates the need for throwing exceptions, and reduces risk of runtime ......
Read more >
Changelog - Cypress Documentation
An error will no longer throw warning that cypress isn't a module after migrating a project's configuration file when using the direct download...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found