Skip to content

Add Error.prototype.cause #47020

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 7, 2022
Merged
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
3 changes: 2 additions & 1 deletion src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,8 @@ namespace ts {
Float64Array: ["at"],
BigInt64Array: ["at"],
BigUint64Array: ["at"],
ObjectConstructor: ["hasOwn"]
ObjectConstructor: ["hasOwn"],
Error: ["cause"]
}
};
}
Expand Down
53 changes: 50 additions & 3 deletions src/lib/es2022.error.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,55 @@
interface ErrorOptions {
cause?: Error;
cause?: Error;

Choose a reason for hiding this comment

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

cause should be typed as unknown (or any in a less strict way) as the value being thrown in JavaScript can be arbitrary values.

}

interface Error {
cause?: Error;
}

interface ErrorConstructor {
new(message?: string, options?: ErrorOptions): Error;
(message?: string, options?: ErrorOptions): Error;
new (message?: string, options?: ErrorOptions): Error;
(message?: string, options?: ErrorOptions): Error;
}

interface EvalErrorConstructor {
new (message?: string, options?: ErrorOptions): EvalError;
(message?: string, options?: ErrorOptions): EvalError;
}

interface RangeErrorConstructor {
new (message?: string, options?: ErrorOptions): RangeError;
(message?: string, options?: ErrorOptions): RangeError;
}

interface ReferenceErrorConstructor {
new (message?: string, options?: ErrorOptions): ReferenceError;
(message?: string, options?: ErrorOptions): ReferenceError;
}

interface SyntaxErrorConstructor {
new (message?: string, options?: ErrorOptions): SyntaxError;
(message?: string, options?: ErrorOptions): SyntaxError;
}

interface TypeErrorConstructor {
new (message?: string, options?: ErrorOptions): TypeError;
(message?: string, options?: ErrorOptions): TypeError;
}

interface URIErrorConstructor {
new (message?: string, options?: ErrorOptions): URIError;
(message?: string, options?: ErrorOptions): URIError;
}

interface AggregateErrorConstructor {
new (
errors: Iterable<any>,
message?: string,
options?: ErrorOptions
): AggregateError;
(
errors: Iterable<any>,
message?: string,
options?: ErrorOptions
): AggregateError;
}
2 changes: 1 addition & 1 deletion tests/baselines/reference/awaitedTypeStrictNull.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ class Api<D = {}> {
>D : Symbol(D, Decl(awaitedTypeStrictNull.ts, 47, 15))
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --))
>D : Symbol(D, Decl(awaitedTypeStrictNull.ts, 47, 15))
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --))
}

declare const api: Api;
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/customAsyncIterator.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ConstantIterator<T> implements AsyncIterator<T, undefined, T | undefined>
>value : Symbol(value, Decl(customAsyncIterator.ts, 4, 15))

throw new Error("ConstantIterator.prototype.next may not take any values");
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --))
}
return { value: this.constant, done: false };
>value : Symbol(value, Decl(customAsyncIterator.ts, 8, 16))
Expand Down
41 changes: 37 additions & 4 deletions tests/baselines/reference/errorCause(target=es2021).errors.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,41 @@
tests/cases/compiler/errorCause.ts(1,18): error TS2554: Expected 0-1 arguments, but got 2.
tests/cases/compiler/errorCause.ts(1,28): error TS2554: Expected 0-1 arguments, but got 2.
tests/cases/compiler/errorCause.ts(2,5): error TS2550: Property 'cause' does not exist on type 'Error'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2022' or later.
tests/cases/compiler/errorCause.ts(4,22): error TS2554: Expected 0-1 arguments, but got 2.
tests/cases/compiler/errorCause.ts(5,23): error TS2554: Expected 0-1 arguments, but got 2.
tests/cases/compiler/errorCause.ts(6,27): error TS2554: Expected 0-1 arguments, but got 2.
tests/cases/compiler/errorCause.ts(7,24): error TS2554: Expected 0-1 arguments, but got 2.
tests/cases/compiler/errorCause.ts(8,22): error TS2554: Expected 0-1 arguments, but got 2.
tests/cases/compiler/errorCause.ts(9,21): error TS2554: Expected 0-1 arguments, but got 2.
tests/cases/compiler/errorCause.ts(10,31): error TS2554: Expected 1-2 arguments, but got 3.


==== tests/cases/compiler/errorCause.ts (1 errors) ====
new Error("foo", { cause: new Error("bar") });
~~~~~~~~~~~~~~~~~~~~~~~~~~~
==== tests/cases/compiler/errorCause.ts (9 errors) ====
let err = new Error("foo", { cause: new Error("bar") });
~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2554: Expected 0-1 arguments, but got 2.
err.cause;
~~~~~
!!! error TS2550: Property 'cause' does not exist on type 'Error'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2022' or later.

new EvalError("foo", { cause: new Error("bar") });
~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2554: Expected 0-1 arguments, but got 2.
new RangeError("foo", { cause: new Error("bar") });
~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2554: Expected 0-1 arguments, but got 2.
new ReferenceError("foo", { cause: new Error("bar") });
~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2554: Expected 0-1 arguments, but got 2.
new SyntaxError("foo", { cause: new Error("bar") });
~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2554: Expected 0-1 arguments, but got 2.
new TypeError("foo", { cause: new Error("bar") });
~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2554: Expected 0-1 arguments, but got 2.
new URIError("foo", { cause: new Error("bar") });
~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2554: Expected 0-1 arguments, but got 2.
new AggregateError([], "foo", { cause: new Error("bar") });
~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2554: Expected 1-2 arguments, but got 3.

21 changes: 19 additions & 2 deletions tests/baselines/reference/errorCause(target=es2021).js
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
//// [errorCause.ts]
new Error("foo", { cause: new Error("bar") });
let err = new Error("foo", { cause: new Error("bar") });
err.cause;

new EvalError("foo", { cause: new Error("bar") });
new RangeError("foo", { cause: new Error("bar") });
new ReferenceError("foo", { cause: new Error("bar") });
new SyntaxError("foo", { cause: new Error("bar") });
new TypeError("foo", { cause: new Error("bar") });
new URIError("foo", { cause: new Error("bar") });
new AggregateError([], "foo", { cause: new Error("bar") });


//// [errorCause.js]
new Error("foo", { cause: new Error("bar") });
let err = new Error("foo", { cause: new Error("bar") });
err.cause;
new EvalError("foo", { cause: new Error("bar") });
new RangeError("foo", { cause: new Error("bar") });
new ReferenceError("foo", { cause: new Error("bar") });
new SyntaxError("foo", { cause: new Error("bar") });
new TypeError("foo", { cause: new Error("bar") });
new URIError("foo", { cause: new Error("bar") });
new AggregateError([], "foo", { cause: new Error("bar") });
43 changes: 41 additions & 2 deletions tests/baselines/reference/errorCause(target=es2021).symbols
Original file line number Diff line number Diff line change
@@ -1,6 +1,45 @@
=== tests/cases/compiler/errorCause.ts ===
new Error("foo", { cause: new Error("bar") });
let err = new Error("foo", { cause: new Error("bar") });
>err : Symbol(err, Decl(errorCause.ts, 0, 3))
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>cause : Symbol(cause, Decl(errorCause.ts, 0, 18))
>cause : Symbol(cause, Decl(errorCause.ts, 0, 28))
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

err.cause;
>err : Symbol(err, Decl(errorCause.ts, 0, 3))

new EvalError("foo", { cause: new Error("bar") });
>EvalError : Symbol(EvalError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>cause : Symbol(cause, Decl(errorCause.ts, 3, 22))
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

new RangeError("foo", { cause: new Error("bar") });
>RangeError : Symbol(RangeError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>cause : Symbol(cause, Decl(errorCause.ts, 4, 23))
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

new ReferenceError("foo", { cause: new Error("bar") });
>ReferenceError : Symbol(ReferenceError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>cause : Symbol(cause, Decl(errorCause.ts, 5, 27))
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

new SyntaxError("foo", { cause: new Error("bar") });
>SyntaxError : Symbol(SyntaxError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>cause : Symbol(cause, Decl(errorCause.ts, 6, 24))
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

new TypeError("foo", { cause: new Error("bar") });
>TypeError : Symbol(TypeError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>cause : Symbol(cause, Decl(errorCause.ts, 7, 22))
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

new URIError("foo", { cause: new Error("bar") });
>URIError : Symbol(URIError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>cause : Symbol(cause, Decl(errorCause.ts, 8, 21))
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

new AggregateError([], "foo", { cause: new Error("bar") });
>AggregateError : Symbol(AggregateError, Decl(lib.es2021.promise.d.ts, --, --), Decl(lib.es2021.promise.d.ts, --, --))
>cause : Symbol(cause, Decl(errorCause.ts, 9, 31))
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

79 changes: 78 additions & 1 deletion tests/baselines/reference/errorCause(target=es2021).types
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
=== tests/cases/compiler/errorCause.ts ===
new Error("foo", { cause: new Error("bar") });
let err = new Error("foo", { cause: new Error("bar") });
>err : Error
>new Error("foo", { cause: new Error("bar") }) : Error
>Error : ErrorConstructor
>"foo" : "foo"
Expand All @@ -9,3 +10,79 @@ new Error("foo", { cause: new Error("bar") });
>Error : ErrorConstructor
>"bar" : "bar"

err.cause;
>err.cause : any
>err : Error
>cause : any

new EvalError("foo", { cause: new Error("bar") });
>new EvalError("foo", { cause: new Error("bar") }) : EvalError & Error
>EvalError : EvalErrorConstructor
>"foo" : "foo"
>{ cause: new Error("bar") } : { cause: Error; }
>cause : Error
>new Error("bar") : Error
>Error : ErrorConstructor
>"bar" : "bar"

new RangeError("foo", { cause: new Error("bar") });
>new RangeError("foo", { cause: new Error("bar") }) : RangeError & Error
>RangeError : RangeErrorConstructor
>"foo" : "foo"
>{ cause: new Error("bar") } : { cause: Error; }
>cause : Error
>new Error("bar") : Error
>Error : ErrorConstructor
>"bar" : "bar"

new ReferenceError("foo", { cause: new Error("bar") });
>new ReferenceError("foo", { cause: new Error("bar") }) : ReferenceError & Error
>ReferenceError : ReferenceErrorConstructor
>"foo" : "foo"
>{ cause: new Error("bar") } : { cause: Error; }
>cause : Error
>new Error("bar") : Error
>Error : ErrorConstructor
>"bar" : "bar"

new SyntaxError("foo", { cause: new Error("bar") });
>new SyntaxError("foo", { cause: new Error("bar") }) : SyntaxError & Error
>SyntaxError : SyntaxErrorConstructor
>"foo" : "foo"
>{ cause: new Error("bar") } : { cause: Error; }
>cause : Error
>new Error("bar") : Error
>Error : ErrorConstructor
>"bar" : "bar"

new TypeError("foo", { cause: new Error("bar") });
>new TypeError("foo", { cause: new Error("bar") }) : TypeError & Error
>TypeError : TypeErrorConstructor
>"foo" : "foo"
>{ cause: new Error("bar") } : { cause: Error; }
>cause : Error
>new Error("bar") : Error
>Error : ErrorConstructor
>"bar" : "bar"

new URIError("foo", { cause: new Error("bar") });
>new URIError("foo", { cause: new Error("bar") }) : URIError & Error
>URIError : URIErrorConstructor
>"foo" : "foo"
>{ cause: new Error("bar") } : { cause: Error; }
>cause : Error
>new Error("bar") : Error
>Error : ErrorConstructor
>"bar" : "bar"

new AggregateError([], "foo", { cause: new Error("bar") });
>new AggregateError([], "foo", { cause: new Error("bar") }) : AggregateError
>AggregateError : AggregateErrorConstructor
>[] : undefined[]
>"foo" : "foo"
>{ cause: new Error("bar") } : { cause: Error; }
>cause : Error
>new Error("bar") : Error
>Error : ErrorConstructor
>"bar" : "bar"

21 changes: 19 additions & 2 deletions tests/baselines/reference/errorCause(target=es2022).js
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
//// [errorCause.ts]
new Error("foo", { cause: new Error("bar") });
let err = new Error("foo", { cause: new Error("bar") });
err.cause;

new EvalError("foo", { cause: new Error("bar") });
new RangeError("foo", { cause: new Error("bar") });
new ReferenceError("foo", { cause: new Error("bar") });
new SyntaxError("foo", { cause: new Error("bar") });
new TypeError("foo", { cause: new Error("bar") });
new URIError("foo", { cause: new Error("bar") });
new AggregateError([], "foo", { cause: new Error("bar") });


//// [errorCause.js]
new Error("foo", { cause: new Error("bar") });
let err = new Error("foo", { cause: new Error("bar") });
err.cause;
new EvalError("foo", { cause: new Error("bar") });
new RangeError("foo", { cause: new Error("bar") });
new ReferenceError("foo", { cause: new Error("bar") });
new SyntaxError("foo", { cause: new Error("bar") });
new TypeError("foo", { cause: new Error("bar") });
new URIError("foo", { cause: new Error("bar") });
new AggregateError([], "foo", { cause: new Error("bar") });
49 changes: 45 additions & 4 deletions tests/baselines/reference/errorCause(target=es2022).symbols
Original file line number Diff line number Diff line change
@@ -1,6 +1,47 @@
=== tests/cases/compiler/errorCause.ts ===
new Error("foo", { cause: new Error("bar") });
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>cause : Symbol(cause, Decl(errorCause.ts, 0, 18))
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
let err = new Error("foo", { cause: new Error("bar") });
>err : Symbol(err, Decl(errorCause.ts, 0, 3))
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --))
>cause : Symbol(cause, Decl(errorCause.ts, 0, 28))
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --))

err.cause;
>err.cause : Symbol(Error.cause, Decl(lib.es2022.error.d.ts, --, --))
>err : Symbol(err, Decl(errorCause.ts, 0, 3))
>cause : Symbol(Error.cause, Decl(lib.es2022.error.d.ts, --, --))

new EvalError("foo", { cause: new Error("bar") });
>EvalError : Symbol(EvalError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>cause : Symbol(cause, Decl(errorCause.ts, 3, 22))
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --))

new RangeError("foo", { cause: new Error("bar") });
>RangeError : Symbol(RangeError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>cause : Symbol(cause, Decl(errorCause.ts, 4, 23))
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --))

new ReferenceError("foo", { cause: new Error("bar") });
>ReferenceError : Symbol(ReferenceError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>cause : Symbol(cause, Decl(errorCause.ts, 5, 27))
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --))

new SyntaxError("foo", { cause: new Error("bar") });
>SyntaxError : Symbol(SyntaxError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>cause : Symbol(cause, Decl(errorCause.ts, 6, 24))
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --))

new TypeError("foo", { cause: new Error("bar") });
>TypeError : Symbol(TypeError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>cause : Symbol(cause, Decl(errorCause.ts, 7, 22))
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --))

new URIError("foo", { cause: new Error("bar") });
>URIError : Symbol(URIError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>cause : Symbol(cause, Decl(errorCause.ts, 8, 21))
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --))

new AggregateError([], "foo", { cause: new Error("bar") });
>AggregateError : Symbol(AggregateError, Decl(lib.es2021.promise.d.ts, --, --), Decl(lib.es2021.promise.d.ts, --, --))
>cause : Symbol(cause, Decl(errorCause.ts, 9, 31))
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --))

Loading