33//// [instanceofOperatorWithRHSHasSymbolHasInstance.ts]
44interface Point { x : number , y : number }
55interface Point3D { x : number , y : number , z : number }
6+ interface Point3D2 extends Point { z : number }
67interface Line { start : Point , end : Point }
78
89declare var rhs0: { [ Symbol . hasInstance ] ( value : unknown ) : boolean ; } ;
@@ -25,6 +26,7 @@ declare var lhs0: any;
2526declare var lhs1 : object ;
2627declare var lhs2 : Point | Point3D | Line ;
2728declare var lhs3 : Point3D | Line ;
29+ declare var lhs4 : Point | Point3D2 | Line ;
2830
2931lhs0 instanceof rhs0 && lhs0 ;
3032lhs0 instanceof rhs1 && lhs0 ;
@@ -75,7 +77,45 @@ lhs3 instanceof Rhs10 && lhs3;
7577lhs3 instanceof Rhs11 && lhs3 ;
7678lhs3 instanceof Rhs12 && lhs3 ;
7779lhs3 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+ }
79119
80120//// [instanceofOperatorWithRHSHasSymbolHasInstance.js]
81121lhs0 instanceof rhs0 && lhs0 ;
@@ -124,3 +164,21 @@ lhs3 instanceof Rhs10 && lhs3;
124164lhs3 instanceof Rhs11 && lhs3 ;
125165lhs3 instanceof Rhs12 && lhs3 ;
126166lhs3 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