Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tsc/internal/bundled/libs/lib.es5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,7 @@ declare var RegExp: RegExpConstructor;
interface Error {
name: string;
message: string;
stack?: string;
stack?: string | undefined;
}

interface ErrorConstructor {
Expand Down
Original file line number Diff line number Diff line change
@@ -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'.

Original file line number Diff line number Diff line change
@@ -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))

Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ declare const _exports: {
new (status: any): {
name: string;
message: string;
stack?: string;
stack?: string | undefined;
};
};
};
Expand Down
Original file line number Diff line number Diff line change
@@ -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);