diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 2d377350c6..1e8a87bc4d 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4622,8 +4622,8 @@ module ts { // Get the narrowed type of a given symbol at a given location function getNarrowedTypeOfSymbol(symbol: Symbol, node: Node) { var type = getTypeOfSymbol(symbol); - // Only narrow when symbol is variable of a structured type - if (node && (symbol.flags & SymbolFlags.Variable && type.flags & TypeFlags.Structured)) { + // Only narrow when symbol is variable of an object, union, or type parameter type + if (node && symbol.flags & SymbolFlags.Variable && type.flags & (TypeFlags.ObjectType | TypeFlags.Union | TypeFlags.TypeParameter)) { loop: while (node.parent) { var child = node; node = node.parent; @@ -6587,12 +6587,12 @@ module ts { return numberType; } - // Return true if type is any, an object type, a type parameter, or a union type composed of only those kinds of types + // Return true if type an object type, a type parameter, or a union type composed of only those kinds of types function isStructuredType(type: Type): boolean { if (type.flags & TypeFlags.Union) { return !forEach((type).types, t => !isStructuredType(t)); } - return (type.flags & TypeFlags.Structured) !== 0; + return (type.flags & (TypeFlags.ObjectType | TypeFlags.TypeParameter)) !== 0; } function isConstEnumObjectType(type: Type): boolean { diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 844f097e25..7d68d4b5a0 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1240,7 +1240,6 @@ module ts { StringLike = String | StringLiteral, NumberLike = Number | Enum, ObjectType = Class | Interface | Reference | Tuple | Anonymous, - Structured = ObjectType | Union | TypeParameter } // Properties common to all types diff --git a/tests/baselines/reference/typeGuardsWithAny.js b/tests/baselines/reference/typeGuardsWithAny.js index 628ea42518..56676fb6d6 100644 --- a/tests/baselines/reference/typeGuardsWithAny.js +++ b/tests/baselines/reference/typeGuardsWithAny.js @@ -1,12 +1,18 @@ //// [typeGuardsWithAny.ts] var x: any = { p: 0 }; if (x instanceof Object) { - x.p; // No error, type any is not narrowed + x.p; // No error, type any unaffected by type guard +} +else { + x.p; // No error, type any unaffected by type guard } //// [typeGuardsWithAny.js] var x = { p: 0 }; if (x instanceof Object) { - x.p; // No error, type any is not narrowed + x.p; // No error, type any unaffected by type guard +} +else { + x.p; // No error, type any unaffected by type guard } diff --git a/tests/baselines/reference/typeGuardsWithAny.types b/tests/baselines/reference/typeGuardsWithAny.types index d79f813715..ee628c9c24 100644 --- a/tests/baselines/reference/typeGuardsWithAny.types +++ b/tests/baselines/reference/typeGuardsWithAny.types @@ -9,7 +9,13 @@ if (x instanceof Object) { >x : any >Object : ObjectConstructor - x.p; // No error, type any is not narrowed + x.p; // No error, type any unaffected by type guard +>x.p : any +>x : any +>p : any +} +else { + x.p; // No error, type any unaffected by type guard >x.p : any >x : any >p : any diff --git a/tests/cases/conformance/expressions/typeGuards/typeGuardsWithAny.ts b/tests/cases/conformance/expressions/typeGuards/typeGuardsWithAny.ts index 4e58d1a4ac..e7e756ea8e 100644 --- a/tests/cases/conformance/expressions/typeGuards/typeGuardsWithAny.ts +++ b/tests/cases/conformance/expressions/typeGuards/typeGuardsWithAny.ts @@ -1,4 +1,7 @@ var x: any = { p: 0 }; if (x instanceof Object) { - x.p; // No error, type any is not narrowed + x.p; // No error, type any unaffected by type guard +} +else { + x.p; // No error, type any unaffected by type guard }