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.cause could be any? #48338

Closed
conartist6 opened this issue Mar 19, 2022 · 2 comments
Closed

Error.cause could be any? #48338

conartist6 opened this issue Mar 19, 2022 · 2 comments

Comments

@conartist6
Copy link

lib Update Request

Configuration Check

My compilation target is ES2015 and my lib is the default.

Missing / Incorrect Definition

I'm reporting this issue by request of @ljharb.

Error.cause is typed as Error | undefined but could contain any value. For what it's worth, I can definitely see the appeal of restricting it to the kind of value we hope it contains, but I would like to hear how the maintainers feel about the trade-off.

Personally I think it's a pretty nuanced trade off, and I can imagine a few possible types:

  • null | undefined | Error (avoids the risk of null pointers at least)
  • unknown (makes some extra work handling causes, but I'm hoping most users will do that once in library code anyway rather than scatter the logic around their apps. Ultimately the language itself should offer the facilities to print causal chains of errors, though that will only be possible once some other proposals land, notably error.stack.
  • any (the general type for throwables, also used in AggregateError)

Sample Code

try {
  maybeFail();
} catch (e: any) {
  if (e instanceof Error && e.cause) {
    // cause could still technically be anything at all
    // for example in code written with vError it is a function
    console.log(`Caused by: ${e.cause.name}`)
  }
}

Documentation Link

DefinitelyTyped/DefinitelyTyped#59354 (comment)

@ljharb
Copy link
Contributor

ljharb commented Mar 19, 2022

Error causes were designed very carefully to be either absent, undefined (which is different), or any other value - null, an array, a string, etc. the type of cause thus can only be unknown and optional.

@MartinJohns
Copy link
Contributor

Duplicate of #48098.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants