Takeshi Tokugawa YD
Takeshi Tokugawa YD

Reputation: 923

Which data type could contain in `error` parameter of "catch` clause (JavaScript/TypeScript)?

My function:

buildErrorMessage(
  {
    originalError,
   // ...
  }: {
    originalError?: Error;
    // ...
  }): string 
{ 
  /* ... */ 
}

When I try to use this function in catch block, the rule no-unsafe-assignment of typescript-eslint tells me that I trying to assign the value of any type:

try {
  // ...
} catch (error) {
  throw new Error(buildErrorMessage({
    originalError: error,
    // ...
  }));
}

Well, which data error could contain besides Error?

Upvotes: 2

Views: 1581

Answers (1)

Bergi
Bergi

Reputation: 664599

Typescript is right here: any value can be thrown and will get caught in your catch clause. Sure, it's best practice to throw Error instances, but Typescript won't assume that you do that.

Upvotes: 3

Related Questions