Repository: TypeScript
Issue: `Error.stack` type definition in lib.es5.d.ts is incorrect / `exactOptionalPropertyTypes` incompatible
Labels: Domain: lib.d.ts

# Bug Report

<!--
  Please fill in each section completely. Thank you!
-->

### 🔎 Search Terms

<!--
  What search terms did you use when trying to find an existing bug report?
  List them here so people in the future can find this one more easily.
-->

Error.stack, exactOptionalPropertyTypes, ErrnoException

### 🕗 Version & Regression Information

<!-- When did you start seeing this bug occur?

"Bugs" that have existed in TS for a long time are very likely to be FAQs; refer to
  https://github.com/Microsoft/TypeScript/wiki/FAQ#common-bugs-that-arent-bugs

If possible, please try testing the nightly version of TS to see if it's already been fixed.
For npm: `typescript@next`
This is also the 'Nightly' version in the playground: http://www.typescriptlang.org/play/?ts=Nightly

Note: The TypeScript Playground can be used to try older versions of TypeScript.

Please keep and fill in the line that best applies:
-->
- 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

<!--
  A link to a TypeScript Playground "Share" link which shows this behavior

  The TypeScript Workbench can be used for more complex setups, try
  https://www.typescriptlang.org/dev/bug-workbench/

  As a last resort, you can link to a repo, but these will be slower for us to investigate.
-->
[Playground link with relevant code](https://www.typescriptlang.org/play?exactOptionalPropertyTypes=true#code/JYOwLgpgTgZghgYwgAgLIE8DCB7bAbAZTEQGtkBvAKGRuT1AgC5kBnMKUAc0oF9LLQkWIhQYAolCjYoyCAA9IIACYtkEqTKq1WxBCWYYc+IqQA+AV2UQYDJbyA) which is valid according to [the ECMAScript spec](https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-error-constructor) and basically every other JS spec out there as `.stack` isn't standardized (as far as eg. [MDN knows](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/Stack) at least)

Edit: @ljharb has an ECMAScript Stage 1 proposal for [an Error Stacks spec](https://github.com/tc39/proposal-error-stacks).

### 💻 Code

<!-- Please post the relevant code sample here as well-->
```ts
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](https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgL):

```
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](https://github.com/tc39/proposal-error-stacks) defines that the `stack` should be [of type `string|undefined`](https://tc39.es/proposal-error-stacks/#sec-additional-built-in-properties), 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.