Skip to content
Discussion options

You must be logged in to vote

error defaults to unknown because everything can be thrown in javascript. If you're certain that error is always of a specific type, you can do an explicit type assertion:

const handleError = (error: unknown) => {
  const genericError = error as DefaultGenericError
};

the safer way would be to let typescript perform type narrowing by doing runtime checks:

const handleError = (error: unknown) => {
  if (error instanceOf Error) {
    // do something with error.message
  }
};

I've also written about that here: https://tkdodo.eu/blog/react-query-and-type-script#what-about-error

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by akinzalowevidation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet
2 participants