Skip to content

Commit fdadd3c

Browse files
author
Yui T
committed
Fix narrow type for instanceOf and add testcases
1 parent c3e4797 commit fdadd3c

4 files changed

Lines changed: 58 additions & 1 deletion

File tree

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4720,7 +4720,7 @@ module ts {
47204720
return targetType;
47214721
}
47224722
// If current type is a union type, remove all constituents that aren't subtypes of target type
4723-
if (type.flags && TypeFlags.Union) {
4723+
if (type.flags & TypeFlags.Union) {
47244724
return getUnionType(filter((<UnionType>type).types, t => isTypeSubtypeOf(t, targetType)));
47254725
}
47264726
return type;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//// [typeGuardsWithInstanceOf.ts]
2+
interface I { global: string; }
3+
var result: I;
4+
var result2: I;
5+
6+
if (!(result instanceof RegExp)) {
7+
result = result2;
8+
} else if (!result.global) {
9+
}
10+
11+
//// [typeGuardsWithInstanceOf.js]
12+
var result;
13+
var result2;
14+
if (!(result instanceof RegExp)) {
15+
result = result2;
16+
}
17+
else if (!result.global) {
18+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
=== tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOf.ts ===
2+
interface I { global: string; }
3+
>I : I
4+
>global : string
5+
6+
var result: I;
7+
>result : I
8+
>I : I
9+
10+
var result2: I;
11+
>result2 : I
12+
>I : I
13+
14+
if (!(result instanceof RegExp)) {
15+
>!(result instanceof RegExp) : boolean
16+
>(result instanceof RegExp) : boolean
17+
>result instanceof RegExp : boolean
18+
>result : I
19+
>RegExp : RegExpConstructor
20+
21+
result = result2;
22+
>result = result2 : I
23+
>result : I
24+
>result2 : I
25+
26+
} else if (!result.global) {
27+
>!result.global : boolean
28+
>result.global : string
29+
>result : I
30+
>global : string
31+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
interface I { global: string; }
2+
var result: I;
3+
var result2: I;
4+
5+
if (!(result instanceof RegExp)) {
6+
result = result2;
7+
} else if (!result.global) {
8+
}

0 commit comments

Comments
 (0)