Skip to content
Discussion options

You must be logged in to vote

in JavaScript, you can throw anything, it doesn't have to be an Error. If you throw 5, error would be a number. In TypeScript, since v4.4, if you use try/catch, the error you get in the catch clause will also be of type unknown. So you need to narrow it first, e.g. with:

{newUserMutation.error instanceOf Error && <p>{newUserMutation.error?.message}</p>}

I am covering this in my blog here: https://tkdodo.eu/blog/react-query-and-type-script#what-about-error

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@oskar-gmerek
Comment options

@dindustack
Comment options

Answer selected by oskar-gmerek
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants