From 5c4bdb0266fa56080f9cb5f0dc0e5b2d52b87eec Mon Sep 17 00:00:00 2001 From: MartinFerret Date: Mon, 23 Nov 2026 11:45:23 +0100 Subject: [PATCH] Allow undefined in Error.stack under exactOptionalPropertyTypes --- tsc/internal/bundled/libs/lib.es5.d.ts | 2 +- ...StackExactOptionalPropertyTypes.errors.txt | 40 ++++++++++++ ...rorStackExactOptionalPropertyTypes.symbols | 63 +++++++++++++++++++ ...errorStackExactOptionalPropertyTypes.types | 56 +++++++++++++++++ .../jsDeclarationsExportedClassAliases.js | 2 +- .../errorStackExactOptionalPropertyTypes.ts | 31 +++++++++ 6 files changed, 192 insertions(+), 2 deletions(-) create mode 100644 tsc/testdata/baselines/reference/compiler/errorStackExactOptionalPropertyTypes.errors.txt create mode 100644 tsc/testdata/baselines/reference/compiler/errorStackExactOptionalPropertyTypes.symbols create mode 100644 tsc/testdata/baselines/reference/compiler/errorStackExactOptionalPropertyTypes.types create mode 100644 tsc/testdata/tests/cases/compiler/errorStackExactOptionalPropertyTypes.ts diff --git a/tsc/internal/bundled/libs/lib.es5.d.ts b/tsc/internal/bundled/libs/lib.es5.d.ts index 87376e31ae242..37944445f40dd 100644 --- a/tsc/internal/bundled/libs/lib.es5.d.ts +++ b/tsc/internal/bundled/libs/lib.es5.d.ts @@ -1072,7 +1072,7 @@ declare var RegExp: RegExpConstructor; interface Error { name: string; message: string; - stack?: string; + stack?: string | undefined; } interface ErrorConstructor { diff --git a/tsc/testdata/baselines/reference/compiler/errorStackExactOptionalPropertyTypes.errors.txt b/tsc/testdata/baselines/reference/compiler/errorStackExactOptionalPropertyTypes.errors.txt new file mode 100644 index 0000000000000..2b4777faee663 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/errorStackExactOptionalPropertyTypes.errors.txt @@ -0,0 +1,40 @@ +errorStackExactOptionalPropertyTypes.ts(27,16): error TS2379: Argument of type 'Error' is not assignable to parameter of type '{ message: string; stack?: string; }' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties. + Types of property 'stack' are incompatible. + Type 'string | undefined' is not assignable to type 'string'. + Type 'undefined' is not assignable to type 'string'. + + +==== errorStackExactOptionalPropertyTypes.ts (1 errors) ==== + // https://github.com/microsoft/TypeScript/issues/45748 + + // An Error subtype may declare `stack` as possibly undefined. + interface MyError extends Error { + stack: string | undefined; + } + + declare const myError: MyError; + const asError: Error = myError; + + // `stack` may be explicitly set to undefined. + declare const err: Error; + err.stack = undefined; + + // Narrowing `stack` back to `string` in a subtype is still allowed. + interface StrictStackError extends Error { + stack?: string; + } + + class StrictStackErrorClass extends Error { + declare stack?: string; + } + + // Trade-off: an Error is no longer assignable to a type whose optional `stack` + // excludes undefined. + declare function takesErrorLike(e: { message: string; stack?: string }): void; + takesErrorLike(err); + ~~~ +!!! error TS2379: Argument of type 'Error' is not assignable to parameter of type '{ message: string; stack?: string; }' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties. +!!! error TS2379: Types of property 'stack' are incompatible. +!!! error TS2379: Type 'string | undefined' is not assignable to type 'string'. +!!! error TS2379: Type 'undefined' is not assignable to type 'string'. + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/errorStackExactOptionalPropertyTypes.symbols b/tsc/testdata/baselines/reference/compiler/errorStackExactOptionalPropertyTypes.symbols new file mode 100644 index 0000000000000..41bb63b0145b6 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/errorStackExactOptionalPropertyTypes.symbols @@ -0,0 +1,63 @@ +//// [tests/cases/compiler/errorStackExactOptionalPropertyTypes.ts] //// + +=== errorStackExactOptionalPropertyTypes.ts === +// https://github.com/microsoft/TypeScript/issues/45748 + +// An Error subtype may declare `stack` as possibly undefined. +interface MyError extends Error { +>MyError : Symbol(MyError, Decl(errorStackExactOptionalPropertyTypes.ts, 0, 0)) +>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --)) + + stack: string | undefined; +>stack : Symbol(MyError.stack, Decl(errorStackExactOptionalPropertyTypes.ts, 3, 33)) +} + +declare const myError: MyError; +>myError : Symbol(myError, Decl(errorStackExactOptionalPropertyTypes.ts, 7, 13)) +>MyError : Symbol(MyError, Decl(errorStackExactOptionalPropertyTypes.ts, 0, 0)) + +const asError: Error = myError; +>asError : Symbol(asError, Decl(errorStackExactOptionalPropertyTypes.ts, 8, 5)) +>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --)) +>myError : Symbol(myError, Decl(errorStackExactOptionalPropertyTypes.ts, 7, 13)) + +// `stack` may be explicitly set to undefined. +declare const err: Error; +>err : Symbol(err, Decl(errorStackExactOptionalPropertyTypes.ts, 11, 13)) +>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --)) + +err.stack = undefined; +>err.stack : Symbol(Error.stack, Decl(lib.es5.d.ts, --, --)) +>err : Symbol(err, Decl(errorStackExactOptionalPropertyTypes.ts, 11, 13)) +>stack : Symbol(Error.stack, Decl(lib.es5.d.ts, --, --)) +>undefined : Symbol(undefined) + +// Narrowing `stack` back to `string` in a subtype is still allowed. +interface StrictStackError extends Error { +>StrictStackError : Symbol(StrictStackError, Decl(errorStackExactOptionalPropertyTypes.ts, 12, 22)) +>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --)) + + stack?: string; +>stack : Symbol(StrictStackError.stack, Decl(errorStackExactOptionalPropertyTypes.ts, 15, 42)) +} + +class StrictStackErrorClass extends Error { +>StrictStackErrorClass : Symbol(StrictStackErrorClass, Decl(errorStackExactOptionalPropertyTypes.ts, 17, 1)) +>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --)) + + declare stack?: string; +>stack : Symbol(StrictStackErrorClass.stack, Decl(errorStackExactOptionalPropertyTypes.ts, 19, 43)) +} + +// Trade-off: an Error is no longer assignable to a type whose optional `stack` +// excludes undefined. +declare function takesErrorLike(e: { message: string; stack?: string }): void; +>takesErrorLike : Symbol(takesErrorLike, Decl(errorStackExactOptionalPropertyTypes.ts, 21, 1)) +>e : Symbol(e, Decl(errorStackExactOptionalPropertyTypes.ts, 25, 32)) +>message : Symbol(message, Decl(errorStackExactOptionalPropertyTypes.ts, 25, 36)) +>stack : Symbol(stack, Decl(errorStackExactOptionalPropertyTypes.ts, 25, 53)) + +takesErrorLike(err); +>takesErrorLike : Symbol(takesErrorLike, Decl(errorStackExactOptionalPropertyTypes.ts, 21, 1)) +>err : Symbol(err, Decl(errorStackExactOptionalPropertyTypes.ts, 11, 13)) + diff --git a/tsc/testdata/baselines/reference/compiler/errorStackExactOptionalPropertyTypes.types b/tsc/testdata/baselines/reference/compiler/errorStackExactOptionalPropertyTypes.types new file mode 100644 index 0000000000000..baef62fae13aa --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/errorStackExactOptionalPropertyTypes.types @@ -0,0 +1,56 @@ +//// [tests/cases/compiler/errorStackExactOptionalPropertyTypes.ts] //// + +=== errorStackExactOptionalPropertyTypes.ts === +// https://github.com/microsoft/TypeScript/issues/45748 + +// An Error subtype may declare `stack` as possibly undefined. +interface MyError extends Error { + stack: string | undefined; +>stack : string | undefined +} + +declare const myError: MyError; +>myError : MyError + +const asError: Error = myError; +>asError : Error +>myError : MyError + +// `stack` may be explicitly set to undefined. +declare const err: Error; +>err : Error + +err.stack = undefined; +>err.stack = undefined : undefined +>err.stack : string | undefined +>err : Error +>stack : string | undefined +>undefined : undefined + +// Narrowing `stack` back to `string` in a subtype is still allowed. +interface StrictStackError extends Error { + stack?: string; +>stack : string | undefined +} + +class StrictStackErrorClass extends Error { +>StrictStackErrorClass : StrictStackErrorClass +>Error : Error + + declare stack?: string; +>stack : string | undefined +} + +// Trade-off: an Error is no longer assignable to a type whose optional `stack` +// excludes undefined. +declare function takesErrorLike(e: { message: string; stack?: string }): void; +>takesErrorLike : (e: { message: string; stack?: string; }) => void +>e : { message: string; stack?: string; } +>message : string +>stack : string | undefined + +takesErrorLike(err); +>takesErrorLike(err) : void +>takesErrorLike : (e: { message: string; stack?: string; }) => void +>err : Error + diff --git a/tsc/testdata/baselines/reference/conformance/jsDeclarationsExportedClassAliases.js b/tsc/testdata/baselines/reference/conformance/jsDeclarationsExportedClassAliases.js index 524cd12da1405..b3063d1b3ec2b 100644 --- a/tsc/testdata/baselines/reference/conformance/jsDeclarationsExportedClassAliases.js +++ b/tsc/testdata/baselines/reference/conformance/jsDeclarationsExportedClassAliases.js @@ -53,7 +53,7 @@ declare const _exports: { new (status: any): { name: string; message: string; - stack?: string; + stack?: string | undefined; }; }; }; diff --git a/tsc/testdata/tests/cases/compiler/errorStackExactOptionalPropertyTypes.ts b/tsc/testdata/tests/cases/compiler/errorStackExactOptionalPropertyTypes.ts new file mode 100644 index 0000000000000..bdb61df035c4c --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/errorStackExactOptionalPropertyTypes.ts @@ -0,0 +1,31 @@ +// @strict: true +// @exactOptionalPropertyTypes: true +// @noEmit: true + +// https://github.com/microsoft/TypeScript/issues/45748 + +// An Error subtype may declare `stack` as possibly undefined. +interface MyError extends Error { + stack: string | undefined; +} + +declare const myError: MyError; +const asError: Error = myError; + +// `stack` may be explicitly set to undefined. +declare const err: Error; +err.stack = undefined; + +// Narrowing `stack` back to `string` in a subtype is still allowed. +interface StrictStackError extends Error { + stack?: string; +} + +class StrictStackErrorClass extends Error { + declare stack?: string; +} + +// Trade-off: an Error is no longer assignable to a type whose optional `stack` +// excludes undefined. +declare function takesErrorLike(e: { message: string; stack?: string }): void; +takesErrorLike(err);