Fix narrow type for instanceOf and add testcases

This commit is contained in:
Yui T 2015-01-14 11:30:58 -08:00
parent c3e47979f3
commit fdadd3c18e
4 changed files with 58 additions and 1 deletions

View file

@ -4720,7 +4720,7 @@ module ts {
return targetType;
}
// If current type is a union type, remove all constituents that aren't subtypes of target type
if (type.flags && TypeFlags.Union) {
if (type.flags & TypeFlags.Union) {
return getUnionType(filter((<UnionType>type).types, t => isTypeSubtypeOf(t, targetType)));
}
return type;

View file

@ -0,0 +1,18 @@
//// [typeGuardsWithInstanceOf.ts]
interface I { global: string; }
var result: I;
var result2: I;
if (!(result instanceof RegExp)) {
result = result2;
} else if (!result.global) {
}
//// [typeGuardsWithInstanceOf.js]
var result;
var result2;
if (!(result instanceof RegExp)) {
result = result2;
}
else if (!result.global) {
}

View file

@ -0,0 +1,31 @@
=== tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOf.ts ===
interface I { global: string; }
>I : I
>global : string
var result: I;
>result : I
>I : I
var result2: I;
>result2 : I
>I : I
if (!(result instanceof RegExp)) {
>!(result instanceof RegExp) : boolean
>(result instanceof RegExp) : boolean
>result instanceof RegExp : boolean
>result : I
>RegExp : RegExpConstructor
result = result2;
>result = result2 : I
>result : I
>result2 : I
} else if (!result.global) {
>!result.global : boolean
>result.global : string
>result : I
>global : string
}

View file

@ -0,0 +1,8 @@
interface I { global: string; }
var result: I;
var result2: I;
if (!(result instanceof RegExp)) {
result = result2;
} else if (!result.global) {
}