Handling error with typescript problem #3436
-
Hello, I'm learning so please do not beat me ;)
That line of code is working as expected, but when I trying to build a project, then I getting that error:
Is there anyone who will be able help me to sort it out? Kind Regards, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
in JavaScript, you can throw anything, it doesn't have to be an Error. If you
I am covering this in my blog here: https://tkdodo.eu/blog/react-query-and-type-script#what-about-error |
Beta Was this translation helpful? Give feedback.
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 typeunknown
. So you need to narrow it first, e.g. with:I am covering this in my blog here: https://tkdodo.eu/blog/react-query-and-type-script#what-about-error