Skip to content

Commit 8da3eff

Browse files
authored
Subtypes of ErrorConstructor extend it (microsoft#35549)
Previously subtypes of Error extended Error, but the matching subtypes of ErrorConstructor did not extend ErrorConstructor. The members in es5.d.ts are identical, so there's no need except for allowing interface merging into ErrorConstructor to affect subtypes as well.
1 parent 1bbcb55 commit 8da3eff

File tree

5 files changed

+103
-6
lines changed

5 files changed

+103
-6
lines changed

src/lib/es5.d.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,7 @@ declare var Error: ErrorConstructor;
966966
interface EvalError extends Error {
967967
}
968968

969-
interface EvalErrorConstructor {
969+
interface EvalErrorConstructor extends ErrorConstructor {
970970
new(message?: string): EvalError;
971971
(message?: string): EvalError;
972972
readonly prototype: EvalError;
@@ -977,7 +977,7 @@ declare var EvalError: EvalErrorConstructor;
977977
interface RangeError extends Error {
978978
}
979979

980-
interface RangeErrorConstructor {
980+
interface RangeErrorConstructor extends ErrorConstructor {
981981
new(message?: string): RangeError;
982982
(message?: string): RangeError;
983983
readonly prototype: RangeError;
@@ -988,7 +988,7 @@ declare var RangeError: RangeErrorConstructor;
988988
interface ReferenceError extends Error {
989989
}
990990

991-
interface ReferenceErrorConstructor {
991+
interface ReferenceErrorConstructor extends ErrorConstructor {
992992
new(message?: string): ReferenceError;
993993
(message?: string): ReferenceError;
994994
readonly prototype: ReferenceError;
@@ -999,7 +999,7 @@ declare var ReferenceError: ReferenceErrorConstructor;
999999
interface SyntaxError extends Error {
10001000
}
10011001

1002-
interface SyntaxErrorConstructor {
1002+
interface SyntaxErrorConstructor extends ErrorConstructor {
10031003
new(message?: string): SyntaxError;
10041004
(message?: string): SyntaxError;
10051005
readonly prototype: SyntaxError;
@@ -1010,7 +1010,7 @@ declare var SyntaxError: SyntaxErrorConstructor;
10101010
interface TypeError extends Error {
10111011
}
10121012

1013-
interface TypeErrorConstructor {
1013+
interface TypeErrorConstructor extends ErrorConstructor {
10141014
new(message?: string): TypeError;
10151015
(message?: string): TypeError;
10161016
readonly prototype: TypeError;
@@ -1021,7 +1021,7 @@ declare var TypeError: TypeErrorConstructor;
10211021
interface URIError extends Error {
10221022
}
10231023

1024-
interface URIErrorConstructor {
1024+
interface URIErrorConstructor extends ErrorConstructor {
10251025
new(message?: string): URIError;
10261026
(message?: string): URIError;
10271027
readonly prototype: URIError;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//// [errorConstructorSubtypes.ts]
2+
// In Node, ErrorConstructor is augmented with extra properties. Excerpted below.
3+
interface ErrorConstructor {
4+
captureStackTrace(targetObject: Object, constructorOpt?: Function): void;
5+
}
6+
7+
declare var x: ErrorConstructor
8+
x = Error; // OK
9+
x = RangeError;
10+
new x().message
11+
x.captureStackTrace
12+
13+
14+
//// [errorConstructorSubtypes.js]
15+
x = Error; // OK
16+
x = RangeError;
17+
new x().message;
18+
x.captureStackTrace;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
=== tests/cases/compiler/errorConstructorSubtypes.ts ===
2+
// In Node, ErrorConstructor is augmented with extra properties. Excerpted below.
3+
interface ErrorConstructor {
4+
>ErrorConstructor : Symbol(ErrorConstructor, Decl(lib.es5.d.ts, --, --), Decl(errorConstructorSubtypes.ts, 0, 0))
5+
6+
captureStackTrace(targetObject: Object, constructorOpt?: Function): void;
7+
>captureStackTrace : Symbol(ErrorConstructor.captureStackTrace, Decl(errorConstructorSubtypes.ts, 1, 28))
8+
>targetObject : Symbol(targetObject, Decl(errorConstructorSubtypes.ts, 2, 20))
9+
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
10+
>constructorOpt : Symbol(constructorOpt, Decl(errorConstructorSubtypes.ts, 2, 41))
11+
>Function : Symbol(Function, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
12+
}
13+
14+
declare var x: ErrorConstructor
15+
>x : Symbol(x, Decl(errorConstructorSubtypes.ts, 5, 11))
16+
>ErrorConstructor : Symbol(ErrorConstructor, Decl(lib.es5.d.ts, --, --), Decl(errorConstructorSubtypes.ts, 0, 0))
17+
18+
x = Error; // OK
19+
>x : Symbol(x, Decl(errorConstructorSubtypes.ts, 5, 11))
20+
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
21+
22+
x = RangeError;
23+
>x : Symbol(x, Decl(errorConstructorSubtypes.ts, 5, 11))
24+
>RangeError : Symbol(RangeError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
25+
26+
new x().message
27+
>new x().message : Symbol(Error.message, Decl(lib.es5.d.ts, --, --))
28+
>x : Symbol(x, Decl(errorConstructorSubtypes.ts, 5, 11))
29+
>message : Symbol(Error.message, Decl(lib.es5.d.ts, --, --))
30+
31+
x.captureStackTrace
32+
>x.captureStackTrace : Symbol(ErrorConstructor.captureStackTrace, Decl(errorConstructorSubtypes.ts, 1, 28))
33+
>x : Symbol(x, Decl(errorConstructorSubtypes.ts, 5, 11))
34+
>captureStackTrace : Symbol(ErrorConstructor.captureStackTrace, Decl(errorConstructorSubtypes.ts, 1, 28))
35+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
=== tests/cases/compiler/errorConstructorSubtypes.ts ===
2+
// In Node, ErrorConstructor is augmented with extra properties. Excerpted below.
3+
interface ErrorConstructor {
4+
captureStackTrace(targetObject: Object, constructorOpt?: Function): void;
5+
>captureStackTrace : (targetObject: Object, constructorOpt?: Function) => void
6+
>targetObject : Object
7+
>constructorOpt : Function
8+
}
9+
10+
declare var x: ErrorConstructor
11+
>x : ErrorConstructor
12+
13+
x = Error; // OK
14+
>x = Error : ErrorConstructor
15+
>x : ErrorConstructor
16+
>Error : ErrorConstructor
17+
18+
x = RangeError;
19+
>x = RangeError : RangeErrorConstructor
20+
>x : ErrorConstructor
21+
>RangeError : RangeErrorConstructor
22+
23+
new x().message
24+
>new x().message : string
25+
>new x() : Error
26+
>x : ErrorConstructor
27+
>message : string
28+
29+
x.captureStackTrace
30+
>x.captureStackTrace : (targetObject: Object, constructorOpt?: Function) => void
31+
>x : ErrorConstructor
32+
>captureStackTrace : (targetObject: Object, constructorOpt?: Function) => void
33+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @lib: es5,dom
2+
// In Node, ErrorConstructor is augmented with extra properties. Excerpted below.
3+
interface ErrorConstructor {
4+
captureStackTrace(targetObject: Object, constructorOpt?: Function): void;
5+
}
6+
7+
declare var x: ErrorConstructor
8+
x = Error; // OK
9+
x = RangeError;
10+
new x().message
11+
x.captureStackTrace

0 commit comments

Comments
 (0)