Fix narrow type for instanceOf and add testcases
This commit is contained in:
parent
c3e47979f3
commit
fdadd3c18e
|
@ -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;
|
||||
|
|
18
tests/baselines/reference/typeGuardsWithInstanceOf.js
Normal file
18
tests/baselines/reference/typeGuardsWithInstanceOf.js
Normal 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) {
|
||||
}
|
31
tests/baselines/reference/typeGuardsWithInstanceOf.types
Normal file
31
tests/baselines/reference/typeGuardsWithInstanceOf.types
Normal 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
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
interface I { global: string; }
|
||||
var result: I;
|
||||
var result2: I;
|
||||
|
||||
if (!(result instanceof RegExp)) {
|
||||
result = result2;
|
||||
} else if (!result.global) {
|
||||
}
|
Loading…
Reference in a new issue