Skip to content

Commit 363e3a7

Browse files
authored
Add Error.prototype.cause (#47020)
* Add `Error.prototype.cause` Fixes #47019 * Update test baselines
1 parent e46f3ba commit 363e3a7

20 files changed

+531
-38
lines changed

src/compiler/utilities.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,8 @@ namespace ts {
627627
Float64Array: ["at"],
628628
BigInt64Array: ["at"],
629629
BigUint64Array: ["at"],
630-
ObjectConstructor: ["hasOwn"]
630+
ObjectConstructor: ["hasOwn"],
631+
Error: ["cause"]
631632
}
632633
};
633634
}

src/lib/es2022.error.d.ts

+50-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,55 @@
11
interface ErrorOptions {
2-
cause?: Error;
2+
cause?: Error;
3+
}
4+
5+
interface Error {
6+
cause?: Error;
37
}
48

59
interface ErrorConstructor {
6-
new(message?: string, options?: ErrorOptions): Error;
7-
(message?: string, options?: ErrorOptions): Error;
10+
new (message?: string, options?: ErrorOptions): Error;
11+
(message?: string, options?: ErrorOptions): Error;
12+
}
13+
14+
interface EvalErrorConstructor {
15+
new (message?: string, options?: ErrorOptions): EvalError;
16+
(message?: string, options?: ErrorOptions): EvalError;
17+
}
18+
19+
interface RangeErrorConstructor {
20+
new (message?: string, options?: ErrorOptions): RangeError;
21+
(message?: string, options?: ErrorOptions): RangeError;
22+
}
23+
24+
interface ReferenceErrorConstructor {
25+
new (message?: string, options?: ErrorOptions): ReferenceError;
26+
(message?: string, options?: ErrorOptions): ReferenceError;
27+
}
28+
29+
interface SyntaxErrorConstructor {
30+
new (message?: string, options?: ErrorOptions): SyntaxError;
31+
(message?: string, options?: ErrorOptions): SyntaxError;
32+
}
33+
34+
interface TypeErrorConstructor {
35+
new (message?: string, options?: ErrorOptions): TypeError;
36+
(message?: string, options?: ErrorOptions): TypeError;
37+
}
38+
39+
interface URIErrorConstructor {
40+
new (message?: string, options?: ErrorOptions): URIError;
41+
(message?: string, options?: ErrorOptions): URIError;
42+
}
43+
44+
interface AggregateErrorConstructor {
45+
new (
46+
errors: Iterable<any>,
47+
message?: string,
48+
options?: ErrorOptions
49+
): AggregateError;
50+
(
51+
errors: Iterable<any>,
52+
message?: string,
53+
options?: ErrorOptions
54+
): AggregateError;
855
}

tests/baselines/reference/awaitedTypeStrictNull.symbols

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ class Api<D = {}> {
190190
>D : Symbol(D, Decl(awaitedTypeStrictNull.ts, 47, 15))
191191
>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, --, --))
192192
>D : Symbol(D, Decl(awaitedTypeStrictNull.ts, 47, 15))
193-
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
193+
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --))
194194
}
195195

196196
declare const api: Api;

tests/baselines/reference/customAsyncIterator.symbols

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ConstantIterator<T> implements AsyncIterator<T, undefined, T | undefined>
2323
>value : Symbol(value, Decl(customAsyncIterator.ts, 4, 15))
2424

2525
throw new Error("ConstantIterator.prototype.next may not take any values");
26-
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
26+
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --))
2727
}
2828
return { value: this.constant, done: false };
2929
>value : Symbol(value, Decl(customAsyncIterator.ts, 8, 16))
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,41 @@
1-
tests/cases/compiler/errorCause.ts(1,18): error TS2554: Expected 0-1 arguments, but got 2.
1+
tests/cases/compiler/errorCause.ts(1,28): error TS2554: Expected 0-1 arguments, but got 2.
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.
3+
tests/cases/compiler/errorCause.ts(4,22): error TS2554: Expected 0-1 arguments, but got 2.
4+
tests/cases/compiler/errorCause.ts(5,23): error TS2554: Expected 0-1 arguments, but got 2.
5+
tests/cases/compiler/errorCause.ts(6,27): error TS2554: Expected 0-1 arguments, but got 2.
6+
tests/cases/compiler/errorCause.ts(7,24): error TS2554: Expected 0-1 arguments, but got 2.
7+
tests/cases/compiler/errorCause.ts(8,22): error TS2554: Expected 0-1 arguments, but got 2.
8+
tests/cases/compiler/errorCause.ts(9,21): error TS2554: Expected 0-1 arguments, but got 2.
9+
tests/cases/compiler/errorCause.ts(10,31): error TS2554: Expected 1-2 arguments, but got 3.
210

311

4-
==== tests/cases/compiler/errorCause.ts (1 errors) ====
5-
new Error("foo", { cause: new Error("bar") });
6-
~~~~~~~~~~~~~~~~~~~~~~~~~~~
12+
==== tests/cases/compiler/errorCause.ts (9 errors) ====
13+
let err = new Error("foo", { cause: new Error("bar") });
14+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
715
!!! error TS2554: Expected 0-1 arguments, but got 2.
16+
err.cause;
17+
~~~~~
18+
!!! 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.
19+
20+
new EvalError("foo", { cause: new Error("bar") });
21+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
22+
!!! error TS2554: Expected 0-1 arguments, but got 2.
23+
new RangeError("foo", { cause: new Error("bar") });
24+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
25+
!!! error TS2554: Expected 0-1 arguments, but got 2.
26+
new ReferenceError("foo", { cause: new Error("bar") });
27+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
28+
!!! error TS2554: Expected 0-1 arguments, but got 2.
29+
new SyntaxError("foo", { cause: new Error("bar") });
30+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
31+
!!! error TS2554: Expected 0-1 arguments, but got 2.
32+
new TypeError("foo", { cause: new Error("bar") });
33+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
34+
!!! error TS2554: Expected 0-1 arguments, but got 2.
35+
new URIError("foo", { cause: new Error("bar") });
36+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
37+
!!! error TS2554: Expected 0-1 arguments, but got 2.
38+
new AggregateError([], "foo", { cause: new Error("bar") });
39+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
40+
!!! error TS2554: Expected 1-2 arguments, but got 3.
841

Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
11
//// [errorCause.ts]
2-
new Error("foo", { cause: new Error("bar") });
2+
let err = new Error("foo", { cause: new Error("bar") });
3+
err.cause;
4+
5+
new EvalError("foo", { cause: new Error("bar") });
6+
new RangeError("foo", { cause: new Error("bar") });
7+
new ReferenceError("foo", { cause: new Error("bar") });
8+
new SyntaxError("foo", { cause: new Error("bar") });
9+
new TypeError("foo", { cause: new Error("bar") });
10+
new URIError("foo", { cause: new Error("bar") });
11+
new AggregateError([], "foo", { cause: new Error("bar") });
312

413

514
//// [errorCause.js]
6-
new Error("foo", { cause: new Error("bar") });
15+
let err = new Error("foo", { cause: new Error("bar") });
16+
err.cause;
17+
new EvalError("foo", { cause: new Error("bar") });
18+
new RangeError("foo", { cause: new Error("bar") });
19+
new ReferenceError("foo", { cause: new Error("bar") });
20+
new SyntaxError("foo", { cause: new Error("bar") });
21+
new TypeError("foo", { cause: new Error("bar") });
22+
new URIError("foo", { cause: new Error("bar") });
23+
new AggregateError([], "foo", { cause: new Error("bar") });
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,45 @@
11
=== tests/cases/compiler/errorCause.ts ===
2-
new Error("foo", { cause: new Error("bar") });
2+
let err = new Error("foo", { cause: new Error("bar") });
3+
>err : Symbol(err, Decl(errorCause.ts, 0, 3))
34
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
4-
>cause : Symbol(cause, Decl(errorCause.ts, 0, 18))
5+
>cause : Symbol(cause, Decl(errorCause.ts, 0, 28))
6+
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
7+
8+
err.cause;
9+
>err : Symbol(err, Decl(errorCause.ts, 0, 3))
10+
11+
new EvalError("foo", { cause: new Error("bar") });
12+
>EvalError : Symbol(EvalError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
13+
>cause : Symbol(cause, Decl(errorCause.ts, 3, 22))
14+
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
15+
16+
new RangeError("foo", { cause: new Error("bar") });
17+
>RangeError : Symbol(RangeError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
18+
>cause : Symbol(cause, Decl(errorCause.ts, 4, 23))
19+
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
20+
21+
new ReferenceError("foo", { cause: new Error("bar") });
22+
>ReferenceError : Symbol(ReferenceError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
23+
>cause : Symbol(cause, Decl(errorCause.ts, 5, 27))
24+
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
25+
26+
new SyntaxError("foo", { cause: new Error("bar") });
27+
>SyntaxError : Symbol(SyntaxError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
28+
>cause : Symbol(cause, Decl(errorCause.ts, 6, 24))
29+
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
30+
31+
new TypeError("foo", { cause: new Error("bar") });
32+
>TypeError : Symbol(TypeError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
33+
>cause : Symbol(cause, Decl(errorCause.ts, 7, 22))
34+
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
35+
36+
new URIError("foo", { cause: new Error("bar") });
37+
>URIError : Symbol(URIError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
38+
>cause : Symbol(cause, Decl(errorCause.ts, 8, 21))
39+
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
40+
41+
new AggregateError([], "foo", { cause: new Error("bar") });
42+
>AggregateError : Symbol(AggregateError, Decl(lib.es2021.promise.d.ts, --, --), Decl(lib.es2021.promise.d.ts, --, --))
43+
>cause : Symbol(cause, Decl(errorCause.ts, 9, 31))
544
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
645

tests/baselines/reference/errorCause(target=es2021).types

+78-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
=== tests/cases/compiler/errorCause.ts ===
2-
new Error("foo", { cause: new Error("bar") });
2+
let err = new Error("foo", { cause: new Error("bar") });
3+
>err : Error
34
>new Error("foo", { cause: new Error("bar") }) : Error
45
>Error : ErrorConstructor
56
>"foo" : "foo"
@@ -9,3 +10,79 @@ new Error("foo", { cause: new Error("bar") });
910
>Error : ErrorConstructor
1011
>"bar" : "bar"
1112

13+
err.cause;
14+
>err.cause : any
15+
>err : Error
16+
>cause : any
17+
18+
new EvalError("foo", { cause: new Error("bar") });
19+
>new EvalError("foo", { cause: new Error("bar") }) : EvalError & Error
20+
>EvalError : EvalErrorConstructor
21+
>"foo" : "foo"
22+
>{ cause: new Error("bar") } : { cause: Error; }
23+
>cause : Error
24+
>new Error("bar") : Error
25+
>Error : ErrorConstructor
26+
>"bar" : "bar"
27+
28+
new RangeError("foo", { cause: new Error("bar") });
29+
>new RangeError("foo", { cause: new Error("bar") }) : RangeError & Error
30+
>RangeError : RangeErrorConstructor
31+
>"foo" : "foo"
32+
>{ cause: new Error("bar") } : { cause: Error; }
33+
>cause : Error
34+
>new Error("bar") : Error
35+
>Error : ErrorConstructor
36+
>"bar" : "bar"
37+
38+
new ReferenceError("foo", { cause: new Error("bar") });
39+
>new ReferenceError("foo", { cause: new Error("bar") }) : ReferenceError & Error
40+
>ReferenceError : ReferenceErrorConstructor
41+
>"foo" : "foo"
42+
>{ cause: new Error("bar") } : { cause: Error; }
43+
>cause : Error
44+
>new Error("bar") : Error
45+
>Error : ErrorConstructor
46+
>"bar" : "bar"
47+
48+
new SyntaxError("foo", { cause: new Error("bar") });
49+
>new SyntaxError("foo", { cause: new Error("bar") }) : SyntaxError & Error
50+
>SyntaxError : SyntaxErrorConstructor
51+
>"foo" : "foo"
52+
>{ cause: new Error("bar") } : { cause: Error; }
53+
>cause : Error
54+
>new Error("bar") : Error
55+
>Error : ErrorConstructor
56+
>"bar" : "bar"
57+
58+
new TypeError("foo", { cause: new Error("bar") });
59+
>new TypeError("foo", { cause: new Error("bar") }) : TypeError & Error
60+
>TypeError : TypeErrorConstructor
61+
>"foo" : "foo"
62+
>{ cause: new Error("bar") } : { cause: Error; }
63+
>cause : Error
64+
>new Error("bar") : Error
65+
>Error : ErrorConstructor
66+
>"bar" : "bar"
67+
68+
new URIError("foo", { cause: new Error("bar") });
69+
>new URIError("foo", { cause: new Error("bar") }) : URIError & Error
70+
>URIError : URIErrorConstructor
71+
>"foo" : "foo"
72+
>{ cause: new Error("bar") } : { cause: Error; }
73+
>cause : Error
74+
>new Error("bar") : Error
75+
>Error : ErrorConstructor
76+
>"bar" : "bar"
77+
78+
new AggregateError([], "foo", { cause: new Error("bar") });
79+
>new AggregateError([], "foo", { cause: new Error("bar") }) : AggregateError
80+
>AggregateError : AggregateErrorConstructor
81+
>[] : undefined[]
82+
>"foo" : "foo"
83+
>{ cause: new Error("bar") } : { cause: Error; }
84+
>cause : Error
85+
>new Error("bar") : Error
86+
>Error : ErrorConstructor
87+
>"bar" : "bar"
88+
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
11
//// [errorCause.ts]
2-
new Error("foo", { cause: new Error("bar") });
2+
let err = new Error("foo", { cause: new Error("bar") });
3+
err.cause;
4+
5+
new EvalError("foo", { cause: new Error("bar") });
6+
new RangeError("foo", { cause: new Error("bar") });
7+
new ReferenceError("foo", { cause: new Error("bar") });
8+
new SyntaxError("foo", { cause: new Error("bar") });
9+
new TypeError("foo", { cause: new Error("bar") });
10+
new URIError("foo", { cause: new Error("bar") });
11+
new AggregateError([], "foo", { cause: new Error("bar") });
312

413

514
//// [errorCause.js]
6-
new Error("foo", { cause: new Error("bar") });
15+
let err = new Error("foo", { cause: new Error("bar") });
16+
err.cause;
17+
new EvalError("foo", { cause: new Error("bar") });
18+
new RangeError("foo", { cause: new Error("bar") });
19+
new ReferenceError("foo", { cause: new Error("bar") });
20+
new SyntaxError("foo", { cause: new Error("bar") });
21+
new TypeError("foo", { cause: new Error("bar") });
22+
new URIError("foo", { cause: new Error("bar") });
23+
new AggregateError([], "foo", { cause: new Error("bar") });
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,47 @@
11
=== tests/cases/compiler/errorCause.ts ===
2-
new Error("foo", { cause: new Error("bar") });
3-
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
4-
>cause : Symbol(cause, Decl(errorCause.ts, 0, 18))
5-
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
2+
let err = new Error("foo", { cause: new Error("bar") });
3+
>err : Symbol(err, Decl(errorCause.ts, 0, 3))
4+
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --))
5+
>cause : Symbol(cause, Decl(errorCause.ts, 0, 28))
6+
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --))
7+
8+
err.cause;
9+
>err.cause : Symbol(Error.cause, Decl(lib.es2022.error.d.ts, --, --))
10+
>err : Symbol(err, Decl(errorCause.ts, 0, 3))
11+
>cause : Symbol(Error.cause, Decl(lib.es2022.error.d.ts, --, --))
12+
13+
new EvalError("foo", { cause: new Error("bar") });
14+
>EvalError : Symbol(EvalError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
15+
>cause : Symbol(cause, Decl(errorCause.ts, 3, 22))
16+
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --))
17+
18+
new RangeError("foo", { cause: new Error("bar") });
19+
>RangeError : Symbol(RangeError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
20+
>cause : Symbol(cause, Decl(errorCause.ts, 4, 23))
21+
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --))
22+
23+
new ReferenceError("foo", { cause: new Error("bar") });
24+
>ReferenceError : Symbol(ReferenceError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
25+
>cause : Symbol(cause, Decl(errorCause.ts, 5, 27))
26+
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --))
27+
28+
new SyntaxError("foo", { cause: new Error("bar") });
29+
>SyntaxError : Symbol(SyntaxError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
30+
>cause : Symbol(cause, Decl(errorCause.ts, 6, 24))
31+
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --))
32+
33+
new TypeError("foo", { cause: new Error("bar") });
34+
>TypeError : Symbol(TypeError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
35+
>cause : Symbol(cause, Decl(errorCause.ts, 7, 22))
36+
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --))
37+
38+
new URIError("foo", { cause: new Error("bar") });
39+
>URIError : Symbol(URIError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
40+
>cause : Symbol(cause, Decl(errorCause.ts, 8, 21))
41+
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --))
42+
43+
new AggregateError([], "foo", { cause: new Error("bar") });
44+
>AggregateError : Symbol(AggregateError, Decl(lib.es2021.promise.d.ts, --, --), Decl(lib.es2021.promise.d.ts, --, --))
45+
>cause : Symbol(cause, Decl(errorCause.ts, 9, 31))
46+
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --))
647

0 commit comments

Comments
 (0)