|
| 1 | +//// [tests/cases/compiler/controlFlowInstanceofWithSymbolHasInstance.ts] //// |
| 2 | + |
| 3 | +=== controlFlowInstanceofWithSymbolHasInstance.ts === |
| 4 | +interface PromiseConstructor { |
| 5 | + [Symbol.hasInstance](value: any): value is Promise<any>; |
| 6 | +>[Symbol.hasInstance] : (value: any) => value is Promise<any> |
| 7 | +>Symbol.hasInstance : unique symbol |
| 8 | +>Symbol : SymbolConstructor |
| 9 | +>hasInstance : unique symbol |
| 10 | +>value : any |
| 11 | +} |
| 12 | + |
| 13 | +interface SetConstructor { |
| 14 | + [Symbol.hasInstance](value: any): value is Set<any>; |
| 15 | +>[Symbol.hasInstance] : (value: any) => value is Set<any> |
| 16 | +>Symbol.hasInstance : unique symbol |
| 17 | +>Symbol : SymbolConstructor |
| 18 | +>hasInstance : unique symbol |
| 19 | +>value : any |
| 20 | +} |
| 21 | + |
| 22 | +function f1(s: Set<string> | Set<number>) { |
| 23 | +>f1 : (s: Set<string> | Set<number>) => void |
| 24 | +>s : Set<string> | Set<number> |
| 25 | + |
| 26 | + s = new Set<number>(); |
| 27 | +>s = new Set<number>() : Set<number> |
| 28 | +>s : Set<string> | Set<number> |
| 29 | +>new Set<number>() : Set<number> |
| 30 | +>Set : SetConstructor |
| 31 | + |
| 32 | + s; // Set<number> |
| 33 | +>s : Set<number> |
| 34 | + |
| 35 | + if (s instanceof Set) { |
| 36 | +>s instanceof Set : boolean |
| 37 | +>s : Set<number> |
| 38 | +>Set : SetConstructor |
| 39 | + |
| 40 | + s; // Set<number> |
| 41 | +>s : Set<number> |
| 42 | + } |
| 43 | + s; // Set<number> |
| 44 | +>s : Set<number> |
| 45 | + |
| 46 | + s.add(42); |
| 47 | +>s.add(42) : Set<number> |
| 48 | +>s.add : (value: number) => Set<number> |
| 49 | +>s : Set<number> |
| 50 | +>add : (value: number) => Set<number> |
| 51 | +>42 : 42 |
| 52 | +} |
| 53 | + |
| 54 | +function f2(s: Set<string> | Set<number>) { |
| 55 | +>f2 : (s: Set<string> | Set<number>) => void |
| 56 | +>s : Set<string> | Set<number> |
| 57 | + |
| 58 | + s = new Set<number>(); |
| 59 | +>s = new Set<number>() : Set<number> |
| 60 | +>s : Set<string> | Set<number> |
| 61 | +>new Set<number>() : Set<number> |
| 62 | +>Set : SetConstructor |
| 63 | + |
| 64 | + s; // Set<number> |
| 65 | +>s : Set<number> |
| 66 | + |
| 67 | + if (s instanceof Promise) { |
| 68 | +>s instanceof Promise : boolean |
| 69 | +>s : Set<number> |
| 70 | +>Promise : PromiseConstructor |
| 71 | + |
| 72 | + s; // Set<number> & Promise<any> |
| 73 | +>s : Set<number> & Promise<any> |
| 74 | + } |
| 75 | + s; // Set<number> |
| 76 | +>s : Set<number> |
| 77 | + |
| 78 | + s.add(42); |
| 79 | +>s.add(42) : Set<number> |
| 80 | +>s.add : (value: number) => Set<number> |
| 81 | +>s : Set<number> |
| 82 | +>add : (value: number) => Set<number> |
| 83 | +>42 : 42 |
| 84 | +} |
| 85 | + |
| 86 | +function f3(s: Set<string> | Set<number>) { |
| 87 | +>f3 : (s: Set<string> | Set<number>) => void |
| 88 | +>s : Set<string> | Set<number> |
| 89 | + |
| 90 | + s; // Set<string> | Set<number> |
| 91 | +>s : Set<string> | Set<number> |
| 92 | + |
| 93 | + if (s instanceof Set) { |
| 94 | +>s instanceof Set : boolean |
| 95 | +>s : Set<string> | Set<number> |
| 96 | +>Set : SetConstructor |
| 97 | + |
| 98 | + s; // Set<string> | Set<number> |
| 99 | +>s : Set<string> | Set<number> |
| 100 | + } |
| 101 | + else { |
| 102 | + s; // never |
| 103 | +>s : never |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +function f4(s: Set<string> | Set<number>) { |
| 108 | +>f4 : (s: Set<string> | Set<number>) => void |
| 109 | +>s : Set<string> | Set<number> |
| 110 | + |
| 111 | + s = new Set<number>(); |
| 112 | +>s = new Set<number>() : Set<number> |
| 113 | +>s : Set<string> | Set<number> |
| 114 | +>new Set<number>() : Set<number> |
| 115 | +>Set : SetConstructor |
| 116 | + |
| 117 | + s; // Set<number> |
| 118 | +>s : Set<number> |
| 119 | + |
| 120 | + if (s instanceof Set) { |
| 121 | +>s instanceof Set : boolean |
| 122 | +>s : Set<number> |
| 123 | +>Set : SetConstructor |
| 124 | + |
| 125 | + s; // Set<number> |
| 126 | +>s : Set<number> |
| 127 | + } |
| 128 | + else { |
| 129 | + s; // never |
| 130 | +>s : never |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +// More tests |
| 135 | + |
| 136 | +class A { |
| 137 | +>A : A |
| 138 | + |
| 139 | + a: string; |
| 140 | +>a : string |
| 141 | + |
| 142 | + static [Symbol.hasInstance]<T>(this: T, value: unknown): value is ( |
| 143 | +>[Symbol.hasInstance] : <T>(this: T, value: unknown) => value is T extends abstract new (...args: any) => infer U ? U : never |
| 144 | +>Symbol.hasInstance : unique symbol |
| 145 | +>Symbol : SymbolConstructor |
| 146 | +>hasInstance : unique symbol |
| 147 | +>this : T |
| 148 | +>value : unknown |
| 149 | + |
| 150 | + T extends (abstract new (...args: any) => infer U) ? U : |
| 151 | +>args : any |
| 152 | + |
| 153 | + never |
| 154 | + ) { |
| 155 | + return Function.prototype[Symbol.hasInstance].call(this, value); |
| 156 | +>Function.prototype[Symbol.hasInstance].call(this, value) : any |
| 157 | +>Function.prototype[Symbol.hasInstance].call : (this: Function, thisArg: any, ...argArray: any[]) => any |
| 158 | +>Function.prototype[Symbol.hasInstance] : (value: any) => boolean |
| 159 | +>Function.prototype : Function |
| 160 | +>Function : FunctionConstructor |
| 161 | +>prototype : Function |
| 162 | +>Symbol.hasInstance : unique symbol |
| 163 | +>Symbol : SymbolConstructor |
| 164 | +>hasInstance : unique symbol |
| 165 | +>call : (this: Function, thisArg: any, ...argArray: any[]) => any |
| 166 | +>this : T |
| 167 | +>value : unknown |
| 168 | + } |
| 169 | +} |
| 170 | +class B extends A { b: string } |
| 171 | +>B : B |
| 172 | +>A : A |
| 173 | +>b : string |
| 174 | + |
| 175 | +class C extends A { c: string } |
| 176 | +>C : C |
| 177 | +>A : A |
| 178 | +>c : string |
| 179 | + |
| 180 | +function foo(x: A | undefined) { |
| 181 | +>foo : (x: A | undefined) => void |
| 182 | +>x : A | undefined |
| 183 | + |
| 184 | + x; // A | undefined |
| 185 | +>x : A | undefined |
| 186 | + |
| 187 | + if (x instanceof B || x instanceof C) { |
| 188 | +>x instanceof B || x instanceof C : boolean |
| 189 | +>x instanceof B : boolean |
| 190 | +>x : A | undefined |
| 191 | +>B : typeof B |
| 192 | +>x instanceof C : boolean |
| 193 | +>x : A | undefined |
| 194 | +>C : typeof C |
| 195 | + |
| 196 | + x; // B | C |
| 197 | +>x : B | C |
| 198 | + } |
| 199 | + x; // A | undefined |
| 200 | +>x : A | undefined |
| 201 | + |
| 202 | + if (x instanceof B && x instanceof C) { |
| 203 | +>x instanceof B && x instanceof C : boolean |
| 204 | +>x instanceof B : boolean |
| 205 | +>x : A | undefined |
| 206 | +>B : typeof B |
| 207 | +>x instanceof C : boolean |
| 208 | +>x : B |
| 209 | +>C : typeof C |
| 210 | + |
| 211 | + x; // B & C |
| 212 | +>x : B & C |
| 213 | + } |
| 214 | + x; // A | undefined |
| 215 | +>x : A | undefined |
| 216 | + |
| 217 | + if (!x) { |
| 218 | +>!x : boolean |
| 219 | +>x : A | undefined |
| 220 | + |
| 221 | + return; |
| 222 | + } |
| 223 | + x; // A |
| 224 | +>x : A |
| 225 | + |
| 226 | + if (x instanceof B) { |
| 227 | +>x instanceof B : boolean |
| 228 | +>x : A |
| 229 | +>B : typeof B |
| 230 | + |
| 231 | + x; // B |
| 232 | +>x : B |
| 233 | + |
| 234 | + if (x instanceof C) { |
| 235 | +>x instanceof C : boolean |
| 236 | +>x : B |
| 237 | +>C : typeof C |
| 238 | + |
| 239 | + x; // B & C |
| 240 | +>x : B & C |
| 241 | + } |
| 242 | + else { |
| 243 | + x; // B |
| 244 | +>x : B |
| 245 | + } |
| 246 | + x; // B |
| 247 | +>x : B |
| 248 | + } |
| 249 | + else { |
| 250 | + x; // A |
| 251 | +>x : A |
| 252 | + } |
| 253 | + x; // A |
| 254 | +>x : A |
| 255 | +} |
| 256 | + |
| 257 | +// X is neither assignable to Y nor a subtype of Y |
| 258 | +// Y is assignable to X, but not a subtype of X |
| 259 | + |
| 260 | +interface X { |
| 261 | + x?: string; |
| 262 | +>x : string | undefined |
| 263 | +} |
| 264 | + |
| 265 | +class Y { |
| 266 | +>Y : Y |
| 267 | + |
| 268 | + y: string; |
| 269 | +>y : string |
| 270 | +} |
| 271 | + |
| 272 | +function goo(x: X) { |
| 273 | +>goo : (x: X) => void |
| 274 | +>x : X |
| 275 | + |
| 276 | + x; |
| 277 | +>x : X |
| 278 | + |
| 279 | + if (x instanceof Y) { |
| 280 | +>x instanceof Y : boolean |
| 281 | +>x : X |
| 282 | +>Y : typeof Y |
| 283 | + |
| 284 | + x.y; |
| 285 | +>x.y : string |
| 286 | +>x : X & Y |
| 287 | +>y : string |
| 288 | + } |
| 289 | + x; |
| 290 | +>x : X |
| 291 | +} |
| 292 | + |
| 293 | + |
0 commit comments