Home > Back-end >  TypeScript does not show all of the Error constructors
TypeScript does not show all of the Error constructors

Time:09-18

My problem is quite simple: I want to use the Error constructor that takes a message and an options object. Even though my tsconfig.json file states that "target": "es2020", VS Code's Intellisense only shows one constructor for Error (the one that only takes a message). Furthermore, when I navigate to the definition, the file that shows up is called lib.es5.d.ts.

If I try to compile, I get the following message:

src/FetchedConfigDataSource.ts:46:37 - error TS2554: Expected 0-1 arguments, but got 2.

46                 throw new Error('', { cause: err });

What am I doing incorrectly here? Let me know if I should elaborate on anything.

CodePudding user response:

I think error cause wasn't implemented in 2020, I think you need to upgrade to ESNext or similar.

It's still a draft in the 2021 update: https://tc39.es/proposal-error-cause/

This claims it was introduced in es2022.

CodePudding user response:

The options parameter for the Error constructor is not present in ES2020. It was introduced in ES2022 (you can see that it is absent from ES2021). Therefore TypeScript has added this to its typings for ES2022, and if you want this, you will need to use "es2022" or newer for your --target.

Playground link to code

  • Related