Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error Constructor does not accept more than one argument #49535

Closed
moeenn opened this issue Jun 14, 2022 · 4 comments
Closed

Error Constructor does not accept more than one argument #49535

moeenn opened this issue Jun 14, 2022 · 4 comments

Comments

@moeenn
Copy link

moeenn commented Jun 14, 2022

Reading the MDN, I noticed that we can pass multiple arguments to the Error constructor

MDN - Error

  • The first argument is the error message
  • The second argument is an object which includes the property cause

Testing out whether the Node implementation of Error allows this, I came up with this code

const cause = { id: 1, example: "other" }
const err = new Error("Misc. Error description", { cause })

console.log(err.message)  // Misc. Error description
console.log(err.cause) // { id: 1, example: "other" }

I tried instantiating an Error instance as follows

new Error(
  "Required environment variables not set",
  { cause },
)

I got the following error

Expected 0-1 arguments, but got 2.ts(2554)

It appears that the type declarations for Error constructor should be updated to accommodate this.

@MartinJohns
Copy link
Contributor

MartinJohns commented Jun 14, 2022

@moeenn
Copy link
Author

moeenn commented Jun 14, 2022

@MartinJohns I didn't realize cause was a new addition to the standard. Thanks for clarifying this.

@Crusader4Christ
Copy link

Crusader4Christ commented Jul 21, 2023

  • The constructor overload with cause is only supported with ES2022 or later.

In my tsconfig I have

  "compilerOptions": {
    "target": "ES2022",

But anyway I got this error. What the way to fix?

@GabrielDelepine
Copy link

But anyway I got this error. What the way to fix?

Please provide the rest of your tsconfig.json file

@microsoft microsoft locked as resolved and limited conversation to collaborators Nov 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants