Repository navigation
Allow undefined in Error.stack under exactOptionalPropertyTypes - #65636
Martin Ferret (MartinFerret) wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
🔵 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 | undefinedand assignments ofundefinedtoError.stack. - Adds regression coverage, including the intentional incompatibility with narrower
stack?: stringtargets. - 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
|
TypeScript Bot (@typescript-bot) test it |
|
Hey Jake Bailey (@jakebailey), it looks like the DT test run failed. Please check the log for more details. |
|
Jake Bailey (@jakebailey) Here are the results of running the user tests with tsc comparing Something interesting changed - please have a look. Details
|
|
Jake Bailey (@jakebailey), the perf run you requested failed. You can check the log here. |
|
Thanks for running these! The user tests show one new error, in Effect's OtelTracer: an
|
|
Hey Jake Bailey (@jakebailey), the results of running the DT tests are ready. Everything looks the same! |
Fixes #45748
Changes
Error.stackfromstack?: stringtostack?: string | undefinedin lib.es5.d.ts.
Under
exactOptionalPropertyTypes, the current declaration rejectsinterface MyError extends Error { stack: string | undefined }(TS2430),assigning such a subtype to
Error(TS2375), anderr.stack = undefined(TS2412). All three compile with this change. Reading
err.stackisunchanged (
string | undefined), and subtypes declaringstack?: stringstill compile.
Trade-off: with
exactOptionalPropertyTypes, anErroris no longerassignable to a type that declares
stack?: string(e.g.{ message: string; stack?: string }), which now reports TS2379/TS2375.The new test
errorStackExactOptionalPropertyTypescovers the fixed casesand keeps this new error on purpose to document it.
Other baseline change: declaration emit in
jsDeclarationsExportedClassAliasesnow printsstack?: string | undefinedwhen Error members are inlined.
Could a maintainer run the extended tests (user tests /
DefinitelyTyped) to see how much this affects real code?
Thanks!