mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Merge pull request #1670 from Microsoft/fixTypeGuardWithInstanceOf
Fix narrow type for instanceOf and add testcases
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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) {
|
||||
}
|
||||
@@ -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) {
|
||||
}
|
||||
Reference in New Issue
Block a user