From fdadd3c18e73c77a95670498087385ad836c7136 Mon Sep 17 00:00:00 2001 From: Yui T Date: Wed, 14 Jan 2015 11:30:58 -0800 Subject: [PATCH] Fix narrow type for instanceOf and add testcases --- src/compiler/checker.ts | 2 +- .../reference/typeGuardsWithInstanceOf.js | 18 +++++++++++ .../reference/typeGuardsWithInstanceOf.types | 31 +++++++++++++++++++ .../typeGuards/typeGuardsWithInstanceOf.ts | 8 +++++ 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/typeGuardsWithInstanceOf.js create mode 100644 tests/baselines/reference/typeGuardsWithInstanceOf.types create mode 100644 tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOf.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 00990f0c8ac..ca3c0276406 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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((type).types, t => isTypeSubtypeOf(t, targetType))); } return type; diff --git a/tests/baselines/reference/typeGuardsWithInstanceOf.js b/tests/baselines/reference/typeGuardsWithInstanceOf.js new file mode 100644 index 00000000000..34af7037f0b --- /dev/null +++ b/tests/baselines/reference/typeGuardsWithInstanceOf.js @@ -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) { +} diff --git a/tests/baselines/reference/typeGuardsWithInstanceOf.types b/tests/baselines/reference/typeGuardsWithInstanceOf.types new file mode 100644 index 00000000000..0d7b477faed --- /dev/null +++ b/tests/baselines/reference/typeGuardsWithInstanceOf.types @@ -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 +} diff --git a/tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOf.ts b/tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOf.ts new file mode 100644 index 00000000000..2750eb96ebf --- /dev/null +++ b/tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOf.ts @@ -0,0 +1,8 @@ +interface I { global: string; } +var result: I; +var result2: I; + +if (!(result instanceof RegExp)) { + result = result2; +} else if (!result.global) { +} \ No newline at end of file