3
3
//// [instanceofOperatorWithRHSHasSymbolHasInstance.ts]
4
4
interface Point { x : number , y : number }
5
5
interface Point3D { x : number , y : number , z : number }
6
+ interface Point3D2 extends Point { z : number }
6
7
interface Line { start : Point , end : Point }
7
8
8
9
declare var rhs0: { [ Symbol . hasInstance ] ( value : unknown ) : boolean ; } ;
@@ -25,6 +26,7 @@ declare var lhs0: any;
25
26
declare var lhs1 : object ;
26
27
declare var lhs2 : Point | Point3D | Line ;
27
28
declare var lhs3 : Point3D | Line ;
29
+ declare var lhs4 : Point | Point3D2 | Line ;
28
30
29
31
lhs0 instanceof rhs0 && lhs0 ;
30
32
lhs0 instanceof rhs1 && lhs0 ;
@@ -75,7 +77,45 @@ lhs3 instanceof Rhs10 && lhs3;
75
77
lhs3 instanceof Rhs11 && lhs3 ;
76
78
lhs3 instanceof Rhs12 && lhs3 ;
77
79
lhs3 instanceof Rhs13 && lhs3 ;
78
-
80
+
81
+ lhs4 instanceof rhs0 && lhs4 ;
82
+ lhs4 instanceof rhs1 && lhs4 ;
83
+ lhs4 instanceof rhs2 && lhs4 ;
84
+ lhs4 instanceof rhs3 && lhs4 ;
85
+ lhs4 instanceof rhs4 && lhs4 ;
86
+ lhs4 instanceof rhs5 && lhs4 ;
87
+ lhs4 instanceof Rhs7 && lhs4 ;
88
+ lhs4 instanceof Rhs8 && lhs4 ;
89
+ lhs4 instanceof Rhs9 && lhs4 ;
90
+ lhs4 instanceof Rhs10 && lhs4 ;
91
+ lhs4 instanceof Rhs11 && lhs4 ;
92
+ lhs4 instanceof Rhs12 && lhs4 ;
93
+
94
+ declare class A {
95
+ #x: number ;
96
+
97
+ // approximation of `getInstanceType` behavior, with one caveat: the checker versions unions the return types of
98
+ // all construct signatures, but we have no way of extracting individual construct signatures from a type.
99
+ static [ Symbol . hasInstance ] < T > (this: T, value: unknown): value is (
100
+ T extends globalThis.Function ?
101
+ T extends { readonly prototype : infer U } ?
102
+ boolean extends (U extends never ? true : false) ? // < - tests whether 'U ' is 'any '
103
+ T extends ( abstract new ( ...args : any ) => infer V ) ? V : { } :
104
+ U :
105
+ never :
106
+ never
107
+ ) ;
108
+ }
109
+
110
+ declare class B extends A { #y: number ; }
111
+
112
+ declare const obj : unknown ;
113
+ if ( obj instanceof A ) {
114
+ obj ; // A
115
+ }
116
+ if ( obj instanceof B ) {
117
+ obj ; // B
118
+ }
79
119
80
120
//// [instanceofOperatorWithRHSHasSymbolHasInstance.js]
81
121
lhs0 instanceof rhs0 && lhs0 ;
@@ -124,3 +164,21 @@ lhs3 instanceof Rhs10 && lhs3;
124
164
lhs3 instanceof Rhs11 && lhs3 ;
125
165
lhs3 instanceof Rhs12 && lhs3 ;
126
166
lhs3 instanceof Rhs13 && lhs3 ;
167
+ lhs4 instanceof rhs0 && lhs4 ;
168
+ lhs4 instanceof rhs1 && lhs4 ;
169
+ lhs4 instanceof rhs2 && lhs4 ;
170
+ lhs4 instanceof rhs3 && lhs4 ;
171
+ lhs4 instanceof rhs4 && lhs4 ;
172
+ lhs4 instanceof rhs5 && lhs4 ;
173
+ lhs4 instanceof Rhs7 && lhs4 ;
174
+ lhs4 instanceof Rhs8 && lhs4 ;
175
+ lhs4 instanceof Rhs9 && lhs4 ;
176
+ lhs4 instanceof Rhs10 && lhs4 ;
177
+ lhs4 instanceof Rhs11 && lhs4 ;
178
+ lhs4 instanceof Rhs12 && lhs4 ;
179
+ if ( obj instanceof A ) {
180
+ obj ; // A
181
+ }
182
+ if ( obj instanceof B ) {
183
+ obj ; // B
184
+ }
0 commit comments