Skip to content

Allow undefined in Error.stack under exactOptionalPropertyTypes - #65636

Open
Martin Ferret (MartinFerret) wants to merge 1 commit into
microsoft:mainfrom
MartinFerret:fix-45748-error-stack
Open

Martin Ferret (MartinFerret) wants to merge 1 commit into
microsoft:mainfrom
MartinFerret:fix-45748-error-stack

Conversation

@MartinFerret

@MartinFerret Martin Ferret (MartinFerret) commented Nov 23, 2026 •

Copy link
Copy Markdown

Fixes #45748

Changes Error.stack from stack?: string to stack?: string | undefined
in lib.es5.d.ts.

Under exactOptionalPropertyTypes, the current declaration rejects
interface MyError extends Error { stack: string | undefined } (TS2430),
assigning such a subtype to Error (TS2375), and err.stack = undefined
(TS2412). All three compile with this change. Reading err.stack is
unchanged (string | undefined), and subtypes declaring stack?: string
still compile.

Trade-off: with exactOptionalPropertyTypes, an Error is no longer
assignable to a type that declares stack?: string (e.g.
{ message: string; stack?: string }), which now reports TS2379/TS2375.
The new test errorStackExactOptionalPropertyTypes covers the fixed cases
and keeps this new error on purpose to document it.

Other baseline change: declaration emit in
jsDeclarationsExportedClassAliases now prints stack?: string | undefined
when Error members are inlined.

Could a maintainer run the extended tests (user tests /
DefinitelyTyped) to see how much this affects real code?

Thanks!

@typescript-automation typescript-automation Bot added the For Backlog Bug PRs that fix a backlog bug label Nov 23, 2026
@MartinFerret
Martin Ferret (MartinFerret) marked this pull request as ready for review November 23, 2026 14:25
Copilot AI balanced review requested due to automatic review settings November 23, 2026 14:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The intentional break in Error assignability to stack?: string under exactOptionalPropertyTypes requires maintainer assessment of downstream compatibility using the requested user/DefinitelyTyped tests, which were unavailable in this review.

No open issues 🎉

What changed in this PR

Updates TypeScript’s standard library declaration so Error.stack explicitly allows undefined under exactOptionalPropertyTypes, addressing #45748.

Changes:

  • Allows Error subtypes with stack: string | undefined and assignments of undefined to Error.stack.
  • Adds regression coverage, including the intentional incompatibility with narrower stack?: string targets.
  • Updates type, symbol, diagnostic, and declaration-emit baselines.
File Description
tsc/​testdata/​tests/​cases/​compiler/​errorStackExactOptionalPropertyTypes.ts Adds regression cases for subtype compatibility, assignment, and the documented trade-off.
tsc/​testdata/​baselines/​reference/​conformance/​jsDeclarationsExportedClassAliases.js Updates the emitted declaration for inlined Error members.
tsc/​testdata/​baselines/​reference/​compiler/​errorStackExactOptionalPropertyTypes.types Records expected types for the new test.
tsc/​testdata/​baselines/​reference/​compiler/​errorStackExactOptionalPropertyTypes.symbols Records expected symbol bindings for the new test.
tsc/​testdata/​baselines/​reference/​compiler/​errorStackExactOptionalPropertyTypes.errors.txt Records the intentional error for a narrower optional-stack target.
tsc/​internal/​bundled/​libs/​lib.es5.d.ts Changes Error.stack to `stack?: string

Review effort: Balanced

@jakebailey

Copy link
Copy Markdown
Member

TypeScript Bot (@typescript-bot) test it

@typescript-automation

typescript-automation Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Starting jobs; this comment will be updated as builds start and complete.

Command Status Results
test top400 ✅ Started
user test this ✅ Started 👀 Results
run dt ✅ Started ❌ Results
perf test this faster ✅ Started ❌ Results

@typescript-automation

Copy link
Copy Markdown
Contributor

Hey Jake Bailey (@jakebailey), it looks like the DT test run failed. Please check the log for more details.

You can check the log here.

@typescript-automation

Copy link
Copy Markdown
Contributor

Jake Bailey (@jakebailey) Here are the results of running the user tests with tsc comparing baseline and pr:

Something interesting changed - please have a look.

Details

effect

packages/platform/cloudflare/tsconfig.alchemy.json

@typescript-automation

Copy link
Copy Markdown
Contributor

Jake Bailey (@jakebailey), the perf run you requested failed. You can check the log here.

@MartinFerret

Copy link
Copy Markdown
Author

Thanks for running these! The user tests show one new error, in Effect's OtelTracer: an Error passed to @opentelemetry/api's Exception, which declares stack?: string. That's the trade-off described above.

@opentelemetry/api could declare stack?: string | undefined, which works with both the current and the new lib. Would it be ok?

@jakebailey

Copy link
Copy Markdown
Member

TypeScript Bot (@typescript-bot) run dt

@typescript-automation

typescript-automation Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Starting jobs; this comment will be updated as builds start and complete.

Command Status Results
run dt ✅ Started ✅ Results

@typescript-automation

Copy link
Copy Markdown
Contributor

Hey Jake Bailey (@jakebailey), the results of running the DT tests are ready.

Everything looks the same!

You can check the log here.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Backlog Bug PRs that fix a backlog bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants