mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-30 01:04:49 -05:00
Add regression test
This commit is contained in:
156
tests/baselines/reference/controlFlowInstanceof.js
Normal file
156
tests/baselines/reference/controlFlowInstanceof.js
Normal file
@@ -0,0 +1,156 @@
|
||||
//// [controlFlowInstanceof.ts]
|
||||
|
||||
// Repros from #10167
|
||||
|
||||
function f1(s: Set<string> | Set<number>) {
|
||||
s = new Set<number>();
|
||||
s; // Set<number>
|
||||
if (s instanceof Set) {
|
||||
s; // Set<number>
|
||||
}
|
||||
s; // Set<number>
|
||||
s.add(42);
|
||||
}
|
||||
|
||||
function f2(s: Set<string> | Set<number>) {
|
||||
s = new Set<number>();
|
||||
s; // Set<number>
|
||||
if (s instanceof Promise) {
|
||||
s; // Set<number> & Promise<any>
|
||||
}
|
||||
s; // Set<number>
|
||||
s.add(42);
|
||||
}
|
||||
|
||||
function f3(s: Set<string> | Set<number>) {
|
||||
s; // Set<string> | Set<number>
|
||||
if (s instanceof Set) {
|
||||
s; // Set<string> | Set<number>
|
||||
}
|
||||
else {
|
||||
s; // never
|
||||
}
|
||||
}
|
||||
|
||||
function f4(s: Set<string> | Set<number>) {
|
||||
s = new Set<number>();
|
||||
s; // Set<number>
|
||||
if (s instanceof Set) {
|
||||
s; // Set<number>
|
||||
}
|
||||
else {
|
||||
s; // never
|
||||
}
|
||||
}
|
||||
|
||||
// More tests
|
||||
|
||||
class A { a: string }
|
||||
class B extends A { b: string }
|
||||
class C extends A { c: string }
|
||||
|
||||
function foo(x: A | undefined) {
|
||||
x; // A | undefined
|
||||
if (x instanceof B || x instanceof C) {
|
||||
x; // B | C
|
||||
}
|
||||
x; // A | undefined
|
||||
if (x instanceof B && x instanceof C) {
|
||||
x; // B & C
|
||||
}
|
||||
x; // A | undefined
|
||||
if (!x) {
|
||||
return;
|
||||
}
|
||||
x; // A
|
||||
if (x instanceof B) {
|
||||
x; // B
|
||||
if (x instanceof C) {
|
||||
x; // B & C
|
||||
}
|
||||
else {
|
||||
x; // B
|
||||
}
|
||||
x; // B
|
||||
}
|
||||
else {
|
||||
x; // A
|
||||
}
|
||||
x; // A
|
||||
}
|
||||
|
||||
//// [controlFlowInstanceof.js]
|
||||
// Repros from #10167
|
||||
function f1(s) {
|
||||
s = new Set();
|
||||
s; // Set<number>
|
||||
if (s instanceof Set) {
|
||||
s; // Set<number>
|
||||
}
|
||||
s; // Set<number>
|
||||
s.add(42);
|
||||
}
|
||||
function f2(s) {
|
||||
s = new Set();
|
||||
s; // Set<number>
|
||||
if (s instanceof Promise) {
|
||||
s; // Set<number> & Promise<any>
|
||||
}
|
||||
s; // Set<number>
|
||||
s.add(42);
|
||||
}
|
||||
function f3(s) {
|
||||
s; // Set<string> | Set<number>
|
||||
if (s instanceof Set) {
|
||||
s; // Set<string> | Set<number>
|
||||
}
|
||||
else {
|
||||
s; // never
|
||||
}
|
||||
}
|
||||
function f4(s) {
|
||||
s = new Set();
|
||||
s; // Set<number>
|
||||
if (s instanceof Set) {
|
||||
s; // Set<number>
|
||||
}
|
||||
else {
|
||||
s; // never
|
||||
}
|
||||
}
|
||||
// More tests
|
||||
class A {
|
||||
}
|
||||
class B extends A {
|
||||
}
|
||||
class C extends A {
|
||||
}
|
||||
function foo(x) {
|
||||
x; // A | undefined
|
||||
if (x instanceof B || x instanceof C) {
|
||||
x; // B | C
|
||||
}
|
||||
x; // A | undefined
|
||||
if (x instanceof B && x instanceof C) {
|
||||
x; // B & C
|
||||
}
|
||||
x; // A | undefined
|
||||
if (!x) {
|
||||
return;
|
||||
}
|
||||
x; // A
|
||||
if (x instanceof B) {
|
||||
x; // B
|
||||
if (x instanceof C) {
|
||||
x; // B & C
|
||||
}
|
||||
else {
|
||||
x; // B
|
||||
}
|
||||
x; // B
|
||||
}
|
||||
else {
|
||||
x; // A
|
||||
}
|
||||
x; // A
|
||||
}
|
||||
194
tests/baselines/reference/controlFlowInstanceof.symbols
Normal file
194
tests/baselines/reference/controlFlowInstanceof.symbols
Normal file
@@ -0,0 +1,194 @@
|
||||
=== tests/cases/compiler/controlFlowInstanceof.ts ===
|
||||
|
||||
// Repros from #10167
|
||||
|
||||
function f1(s: Set<string> | Set<number>) {
|
||||
>f1 : Symbol(f1, Decl(controlFlowInstanceof.ts, 0, 0))
|
||||
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 3, 12))
|
||||
>Set : Symbol(Set, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --))
|
||||
>Set : Symbol(Set, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --))
|
||||
|
||||
s = new Set<number>();
|
||||
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 3, 12))
|
||||
>Set : Symbol(Set, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --))
|
||||
|
||||
s; // Set<number>
|
||||
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 3, 12))
|
||||
|
||||
if (s instanceof Set) {
|
||||
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 3, 12))
|
||||
>Set : Symbol(Set, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --))
|
||||
|
||||
s; // Set<number>
|
||||
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 3, 12))
|
||||
}
|
||||
s; // Set<number>
|
||||
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 3, 12))
|
||||
|
||||
s.add(42);
|
||||
>s.add : Symbol(Set.add, Decl(lib.es2015.collection.d.ts, --, --))
|
||||
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 3, 12))
|
||||
>add : Symbol(Set.add, Decl(lib.es2015.collection.d.ts, --, --))
|
||||
}
|
||||
|
||||
function f2(s: Set<string> | Set<number>) {
|
||||
>f2 : Symbol(f2, Decl(controlFlowInstanceof.ts, 11, 1))
|
||||
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 13, 12))
|
||||
>Set : Symbol(Set, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --))
|
||||
>Set : Symbol(Set, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --))
|
||||
|
||||
s = new Set<number>();
|
||||
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 13, 12))
|
||||
>Set : Symbol(Set, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --))
|
||||
|
||||
s; // Set<number>
|
||||
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 13, 12))
|
||||
|
||||
if (s instanceof Promise) {
|
||||
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 13, 12))
|
||||
>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --))
|
||||
|
||||
s; // Set<number> & Promise<any>
|
||||
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 13, 12))
|
||||
}
|
||||
s; // Set<number>
|
||||
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 13, 12))
|
||||
|
||||
s.add(42);
|
||||
>s.add : Symbol(Set.add, Decl(lib.es2015.collection.d.ts, --, --))
|
||||
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 13, 12))
|
||||
>add : Symbol(Set.add, Decl(lib.es2015.collection.d.ts, --, --))
|
||||
}
|
||||
|
||||
function f3(s: Set<string> | Set<number>) {
|
||||
>f3 : Symbol(f3, Decl(controlFlowInstanceof.ts, 21, 1))
|
||||
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 23, 12))
|
||||
>Set : Symbol(Set, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --))
|
||||
>Set : Symbol(Set, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --))
|
||||
|
||||
s; // Set<string> | Set<number>
|
||||
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 23, 12))
|
||||
|
||||
if (s instanceof Set) {
|
||||
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 23, 12))
|
||||
>Set : Symbol(Set, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --))
|
||||
|
||||
s; // Set<string> | Set<number>
|
||||
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 23, 12))
|
||||
}
|
||||
else {
|
||||
s; // never
|
||||
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 23, 12))
|
||||
}
|
||||
}
|
||||
|
||||
function f4(s: Set<string> | Set<number>) {
|
||||
>f4 : Symbol(f4, Decl(controlFlowInstanceof.ts, 31, 1))
|
||||
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 33, 12))
|
||||
>Set : Symbol(Set, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --))
|
||||
>Set : Symbol(Set, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --))
|
||||
|
||||
s = new Set<number>();
|
||||
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 33, 12))
|
||||
>Set : Symbol(Set, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --))
|
||||
|
||||
s; // Set<number>
|
||||
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 33, 12))
|
||||
|
||||
if (s instanceof Set) {
|
||||
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 33, 12))
|
||||
>Set : Symbol(Set, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --))
|
||||
|
||||
s; // Set<number>
|
||||
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 33, 12))
|
||||
}
|
||||
else {
|
||||
s; // never
|
||||
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 33, 12))
|
||||
}
|
||||
}
|
||||
|
||||
// More tests
|
||||
|
||||
class A { a: string }
|
||||
>A : Symbol(A, Decl(controlFlowInstanceof.ts, 42, 1))
|
||||
>a : Symbol(A.a, Decl(controlFlowInstanceof.ts, 46, 9))
|
||||
|
||||
class B extends A { b: string }
|
||||
>B : Symbol(B, Decl(controlFlowInstanceof.ts, 46, 21))
|
||||
>A : Symbol(A, Decl(controlFlowInstanceof.ts, 42, 1))
|
||||
>b : Symbol(B.b, Decl(controlFlowInstanceof.ts, 47, 19))
|
||||
|
||||
class C extends A { c: string }
|
||||
>C : Symbol(C, Decl(controlFlowInstanceof.ts, 47, 31))
|
||||
>A : Symbol(A, Decl(controlFlowInstanceof.ts, 42, 1))
|
||||
>c : Symbol(C.c, Decl(controlFlowInstanceof.ts, 48, 19))
|
||||
|
||||
function foo(x: A | undefined) {
|
||||
>foo : Symbol(foo, Decl(controlFlowInstanceof.ts, 48, 31))
|
||||
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
|
||||
>A : Symbol(A, Decl(controlFlowInstanceof.ts, 42, 1))
|
||||
|
||||
x; // A | undefined
|
||||
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
|
||||
|
||||
if (x instanceof B || x instanceof C) {
|
||||
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
|
||||
>B : Symbol(B, Decl(controlFlowInstanceof.ts, 46, 21))
|
||||
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
|
||||
>C : Symbol(C, Decl(controlFlowInstanceof.ts, 47, 31))
|
||||
|
||||
x; // B | C
|
||||
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
|
||||
}
|
||||
x; // A | undefined
|
||||
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
|
||||
|
||||
if (x instanceof B && x instanceof C) {
|
||||
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
|
||||
>B : Symbol(B, Decl(controlFlowInstanceof.ts, 46, 21))
|
||||
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
|
||||
>C : Symbol(C, Decl(controlFlowInstanceof.ts, 47, 31))
|
||||
|
||||
x; // B & C
|
||||
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
|
||||
}
|
||||
x; // A | undefined
|
||||
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
|
||||
|
||||
if (!x) {
|
||||
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
|
||||
|
||||
return;
|
||||
}
|
||||
x; // A
|
||||
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
|
||||
|
||||
if (x instanceof B) {
|
||||
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
|
||||
>B : Symbol(B, Decl(controlFlowInstanceof.ts, 46, 21))
|
||||
|
||||
x; // B
|
||||
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
|
||||
|
||||
if (x instanceof C) {
|
||||
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
|
||||
>C : Symbol(C, Decl(controlFlowInstanceof.ts, 47, 31))
|
||||
|
||||
x; // B & C
|
||||
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
|
||||
}
|
||||
else {
|
||||
x; // B
|
||||
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
|
||||
}
|
||||
x; // B
|
||||
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
|
||||
}
|
||||
else {
|
||||
x; // A
|
||||
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
|
||||
}
|
||||
x; // A
|
||||
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
|
||||
}
|
||||
217
tests/baselines/reference/controlFlowInstanceof.types
Normal file
217
tests/baselines/reference/controlFlowInstanceof.types
Normal file
@@ -0,0 +1,217 @@
|
||||
=== tests/cases/compiler/controlFlowInstanceof.ts ===
|
||||
|
||||
// Repros from #10167
|
||||
|
||||
function f1(s: Set<string> | Set<number>) {
|
||||
>f1 : (s: Set<string> | Set<number>) => void
|
||||
>s : Set<string> | Set<number>
|
||||
>Set : Set<T>
|
||||
>Set : Set<T>
|
||||
|
||||
s = new Set<number>();
|
||||
>s = new Set<number>() : Set<number>
|
||||
>s : Set<string> | Set<number>
|
||||
>new Set<number>() : Set<number>
|
||||
>Set : SetConstructor
|
||||
|
||||
s; // Set<number>
|
||||
>s : Set<number>
|
||||
|
||||
if (s instanceof Set) {
|
||||
>s instanceof Set : boolean
|
||||
>s : Set<number>
|
||||
>Set : SetConstructor
|
||||
|
||||
s; // Set<number>
|
||||
>s : Set<number>
|
||||
}
|
||||
s; // Set<number>
|
||||
>s : Set<number>
|
||||
|
||||
s.add(42);
|
||||
>s.add(42) : Set<number>
|
||||
>s.add : (value: number) => Set<number>
|
||||
>s : Set<number>
|
||||
>add : (value: number) => Set<number>
|
||||
>42 : number
|
||||
}
|
||||
|
||||
function f2(s: Set<string> | Set<number>) {
|
||||
>f2 : (s: Set<string> | Set<number>) => void
|
||||
>s : Set<string> | Set<number>
|
||||
>Set : Set<T>
|
||||
>Set : Set<T>
|
||||
|
||||
s = new Set<number>();
|
||||
>s = new Set<number>() : Set<number>
|
||||
>s : Set<string> | Set<number>
|
||||
>new Set<number>() : Set<number>
|
||||
>Set : SetConstructor
|
||||
|
||||
s; // Set<number>
|
||||
>s : Set<number>
|
||||
|
||||
if (s instanceof Promise) {
|
||||
>s instanceof Promise : boolean
|
||||
>s : Set<number>
|
||||
>Promise : PromiseConstructor
|
||||
|
||||
s; // Set<number> & Promise<any>
|
||||
>s : Set<number> & Promise<any>
|
||||
}
|
||||
s; // Set<number>
|
||||
>s : Set<number>
|
||||
|
||||
s.add(42);
|
||||
>s.add(42) : Set<number>
|
||||
>s.add : (value: number) => Set<number>
|
||||
>s : Set<number>
|
||||
>add : (value: number) => Set<number>
|
||||
>42 : number
|
||||
}
|
||||
|
||||
function f3(s: Set<string> | Set<number>) {
|
||||
>f3 : (s: Set<string> | Set<number>) => void
|
||||
>s : Set<string> | Set<number>
|
||||
>Set : Set<T>
|
||||
>Set : Set<T>
|
||||
|
||||
s; // Set<string> | Set<number>
|
||||
>s : Set<string> | Set<number>
|
||||
|
||||
if (s instanceof Set) {
|
||||
>s instanceof Set : boolean
|
||||
>s : Set<string> | Set<number>
|
||||
>Set : SetConstructor
|
||||
|
||||
s; // Set<string> | Set<number>
|
||||
>s : Set<string> | Set<number>
|
||||
}
|
||||
else {
|
||||
s; // never
|
||||
>s : never
|
||||
}
|
||||
}
|
||||
|
||||
function f4(s: Set<string> | Set<number>) {
|
||||
>f4 : (s: Set<string> | Set<number>) => void
|
||||
>s : Set<string> | Set<number>
|
||||
>Set : Set<T>
|
||||
>Set : Set<T>
|
||||
|
||||
s = new Set<number>();
|
||||
>s = new Set<number>() : Set<number>
|
||||
>s : Set<string> | Set<number>
|
||||
>new Set<number>() : Set<number>
|
||||
>Set : SetConstructor
|
||||
|
||||
s; // Set<number>
|
||||
>s : Set<number>
|
||||
|
||||
if (s instanceof Set) {
|
||||
>s instanceof Set : boolean
|
||||
>s : Set<number>
|
||||
>Set : SetConstructor
|
||||
|
||||
s; // Set<number>
|
||||
>s : Set<number>
|
||||
}
|
||||
else {
|
||||
s; // never
|
||||
>s : never
|
||||
}
|
||||
}
|
||||
|
||||
// More tests
|
||||
|
||||
class A { a: string }
|
||||
>A : A
|
||||
>a : string
|
||||
|
||||
class B extends A { b: string }
|
||||
>B : B
|
||||
>A : A
|
||||
>b : string
|
||||
|
||||
class C extends A { c: string }
|
||||
>C : C
|
||||
>A : A
|
||||
>c : string
|
||||
|
||||
function foo(x: A | undefined) {
|
||||
>foo : (x: A) => void
|
||||
>x : A
|
||||
>A : A
|
||||
|
||||
x; // A | undefined
|
||||
>x : A
|
||||
|
||||
if (x instanceof B || x instanceof C) {
|
||||
>x instanceof B || x instanceof C : boolean
|
||||
>x instanceof B : boolean
|
||||
>x : A
|
||||
>B : typeof B
|
||||
>x instanceof C : boolean
|
||||
>x : A
|
||||
>C : typeof C
|
||||
|
||||
x; // B | C
|
||||
>x : B | C
|
||||
}
|
||||
x; // A | undefined
|
||||
>x : A
|
||||
|
||||
if (x instanceof B && x instanceof C) {
|
||||
>x instanceof B && x instanceof C : boolean
|
||||
>x instanceof B : boolean
|
||||
>x : A
|
||||
>B : typeof B
|
||||
>x instanceof C : boolean
|
||||
>x : B
|
||||
>C : typeof C
|
||||
|
||||
x; // B & C
|
||||
>x : B & C
|
||||
}
|
||||
x; // A | undefined
|
||||
>x : A
|
||||
|
||||
if (!x) {
|
||||
>!x : boolean
|
||||
>x : A
|
||||
|
||||
return;
|
||||
}
|
||||
x; // A
|
||||
>x : A
|
||||
|
||||
if (x instanceof B) {
|
||||
>x instanceof B : boolean
|
||||
>x : A
|
||||
>B : typeof B
|
||||
|
||||
x; // B
|
||||
>x : B
|
||||
|
||||
if (x instanceof C) {
|
||||
>x instanceof C : boolean
|
||||
>x : B
|
||||
>C : typeof C
|
||||
|
||||
x; // B & C
|
||||
>x : B & C
|
||||
}
|
||||
else {
|
||||
x; // B
|
||||
>x : B
|
||||
}
|
||||
x; // B
|
||||
>x : B
|
||||
}
|
||||
else {
|
||||
x; // A
|
||||
>x : A
|
||||
}
|
||||
x; // A
|
||||
>x : A
|
||||
}
|
||||
80
tests/cases/compiler/controlFlowInstanceof.ts
Normal file
80
tests/cases/compiler/controlFlowInstanceof.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
// @target: es6
|
||||
|
||||
// Repros from #10167
|
||||
|
||||
function f1(s: Set<string> | Set<number>) {
|
||||
s = new Set<number>();
|
||||
s; // Set<number>
|
||||
if (s instanceof Set) {
|
||||
s; // Set<number>
|
||||
}
|
||||
s; // Set<number>
|
||||
s.add(42);
|
||||
}
|
||||
|
||||
function f2(s: Set<string> | Set<number>) {
|
||||
s = new Set<number>();
|
||||
s; // Set<number>
|
||||
if (s instanceof Promise) {
|
||||
s; // Set<number> & Promise<any>
|
||||
}
|
||||
s; // Set<number>
|
||||
s.add(42);
|
||||
}
|
||||
|
||||
function f3(s: Set<string> | Set<number>) {
|
||||
s; // Set<string> | Set<number>
|
||||
if (s instanceof Set) {
|
||||
s; // Set<string> | Set<number>
|
||||
}
|
||||
else {
|
||||
s; // never
|
||||
}
|
||||
}
|
||||
|
||||
function f4(s: Set<string> | Set<number>) {
|
||||
s = new Set<number>();
|
||||
s; // Set<number>
|
||||
if (s instanceof Set) {
|
||||
s; // Set<number>
|
||||
}
|
||||
else {
|
||||
s; // never
|
||||
}
|
||||
}
|
||||
|
||||
// More tests
|
||||
|
||||
class A { a: string }
|
||||
class B extends A { b: string }
|
||||
class C extends A { c: string }
|
||||
|
||||
function foo(x: A | undefined) {
|
||||
x; // A | undefined
|
||||
if (x instanceof B || x instanceof C) {
|
||||
x; // B | C
|
||||
}
|
||||
x; // A | undefined
|
||||
if (x instanceof B && x instanceof C) {
|
||||
x; // B & C
|
||||
}
|
||||
x; // A | undefined
|
||||
if (!x) {
|
||||
return;
|
||||
}
|
||||
x; // A
|
||||
if (x instanceof B) {
|
||||
x; // B
|
||||
if (x instanceof C) {
|
||||
x; // B & C
|
||||
}
|
||||
else {
|
||||
x; // B
|
||||
}
|
||||
x; // B
|
||||
}
|
||||
else {
|
||||
x; // A
|
||||
}
|
||||
x; // A
|
||||
}
|
||||
Reference in New Issue
Block a user