question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`Error.stack` type definition in lib.es5.d.ts is incorrect / `exactOptionalPropertyTypes` incompatible

See original GitHub issue

Bug Report

🔎 Search Terms

Error.stack, exactOptionalPropertyTypes, ErrnoException

🕗 Version & Regression Information

  • This is a type issue
  • This changed in #8705 but it took until the 4.4 release for it to surface, thanks to exactOptionalPropertyTypes and it making Error incompatible with a corrected Error-subclass in @types/node: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/55425
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about es5

⏯ Playground Link

Playground link with relevant code which is valid according to the ECMAScript spec and basically every other JS spec out there as .stack isn’t standardized (as far as eg. MDN knows at least)

Edit: @ljharb has an ECMAScript Stage 1 proposal for an Error Stacks spec.

💻 Code

interface MyCoolStack {
    line: string
}

interface MyError extends Error {
    stack: MyCoolStack
}

🙁 Actual behavior

Got error:

Interface 'MyError' incorrectly extends interface 'Error'.
  Types of property 'stack' are incompatible.
    Type 'MyCoolStack | undefined' is not assignable to type 'string'.
      Type 'undefined' is not assignable to type 'string'.

🙂 Expected behavior

One of two:

  1. Either that it passes completely…
  2. …or at least that it complains the same as it does with exactOptionalPropertyTypes turned off:
Interface 'MyError' incorrectly extends interface 'Error'.
  Types of property 'stack' are incompatible.
    Type 'MyCoolStack | undefined' is not assignable to type 'string | undefined'.
      Type 'MyCoolStack' is not assignable to type 'string'.

Edit: The ECMAScript Stage 1 proposal for an Error Stacks spec defines that the stack should be of type string|undefined, which makes the current type of stack?:string wrong and should be replaced with stack?:string|undefined if one agrees with that specs view of the current state of Error stacks.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:4
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
MichaelMitchell-atcommented, Mar 29, 2022

Bumping this, as it’s a major blocker to enabling exactOptionalPropertyTypes in our code base

0reactions
ljharbcommented, Sep 7, 2021

That’s for the accessor, if you .call it on a non-error object, for back compat. Fair point tho, that the standard accessor can return undefined.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Interface 'ErrnoException' incorrectly extends ...
d.ts:172:15 - error TS2430: Interface 'ErrnoException' incorrectly extends interface 'Error'. Types of property 'stack' are incompatible. Type ...
Read more >
Typescript incompatible type error, but type is correct. Only ...
I'm trying to use a method decorator to coerce the type of an argument to a method. It seems like it should work....
Read more >
TSConfig Reference - Docs on every TSConfig option
ts files are type definition files which describe the external API of your module. With .d.ts files, tools like TypeScript can provide intellisense...
Read more >
The Definitive TypeScript 4.8 Guide
The TS compiler determines a type for every value (variable, function argument, return value, etc.) in a program, and it uses these types...
Read more >
TypeScript errors and how to fix them
If you want to export a constant from a definition file ( d.ts ) ... is not a valid async function return type...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found