diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 431d81696a1..9e88a6cb01a 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -66,8 +66,8 @@ module ts { var numberType = createIntrinsicType(TypeFlags.Number, "number"); var booleanType = createIntrinsicType(TypeFlags.Boolean, "boolean"); var voidType = createIntrinsicType(TypeFlags.Void, "void"); - var undefinedType = createIntrinsicType(TypeFlags.Undefined | TypeFlags.ContainsUndefined, "undefined"); - var nullType = createIntrinsicType(TypeFlags.Null | TypeFlags.ContainsUndefined, "null"); + var undefinedType = createIntrinsicType(TypeFlags.Undefined | TypeFlags.ContainsUndefinedOrNull, "undefined"); + var nullType = createIntrinsicType(TypeFlags.Null | TypeFlags.ContainsUndefinedOrNull, "null"); var unknownType = createIntrinsicType(TypeFlags.Any, "unknown"); var resolvingType = createIntrinsicType(TypeFlags.Any, "__resolving__"); @@ -2807,6 +2807,9 @@ module ts { } } + // This function is used to propagate widening flags when creating new object types references and union types. + // It is only necessary to do so if a constituent type might be the undefined type, the null type, or the type + // of an object literal (since those types have widening related information we need to track). function getWideningFlagsOfTypes(types: Type[]): TypeFlags { var result: TypeFlags = 0; for (var i = 0; i < types.length; i++) { @@ -4111,7 +4114,7 @@ module ts { var symbol = createSymbol(p.flags | SymbolFlags.Transient, p.name); symbol.declarations = p.declarations; symbol.parent = p.parent; - symbol.type = getWidenedType(getTypeOfSymbol(p)); + symbol.type = widenedType; symbol.target = p; if (p.valueDeclaration) symbol.valueDeclaration = p.valueDeclaration; p = symbol; @@ -4160,7 +4163,7 @@ module ts { var errorReported = false; forEach(getPropertiesOfObjectType(type), p => { var t = getTypeOfSymbol(p); - if (t.flags & TypeFlags.ContainsUndefined) { + if (t.flags & TypeFlags.ContainsUndefinedOrNull) { if (!reportWideningErrorsInType(t)) { error(p.valueDeclaration, Diagnostics.Object_literal_s_property_0_implicitly_has_an_1_type, p.name, typeToString(getWidenedType(t))); } @@ -4204,7 +4207,7 @@ module ts { } function reportErrorsFromWidening(declaration: Declaration, type: Type) { - if (produceDiagnostics && compilerOptions.noImplicitAny && type.flags & TypeFlags.ContainsUndefined) { + if (produceDiagnostics && compilerOptions.noImplicitAny && type.flags & TypeFlags.ContainsUndefinedOrNull) { // Report implicit any error within type if possible, otherwise report error on declaration if (!reportWideningErrorsInType(type)) { reportImplicitAnyError(declaration, type); @@ -5464,7 +5467,7 @@ module ts { var stringIndexType = getIndexType(IndexKind.String); var numberIndexType = getIndexType(IndexKind.Number); var result = createAnonymousType(node.symbol, properties, emptyArray, emptyArray, stringIndexType, numberIndexType); - result.flags |= TypeFlags.ObjectLiteral | TypeFlags.ContainsLiteral | (typeFlags & TypeFlags.ContainsUndefined); + result.flags |= TypeFlags.ObjectLiteral | TypeFlags.ContainsObjectLiteral | (typeFlags & TypeFlags.ContainsUndefinedOrNull); return result; function getIndexType(kind: IndexKind) { diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 474fdf30fed..ad20efcef3e 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1266,32 +1266,32 @@ module ts { } export const enum TypeFlags { - Any = 0x00000001, - String = 0x00000002, - Number = 0x00000004, - Boolean = 0x00000008, - Void = 0x00000010, - Undefined = 0x00000020, - Null = 0x00000040, - Enum = 0x00000080, // Enum type - StringLiteral = 0x00000100, // String literal type - TypeParameter = 0x00000200, // Type parameter - Class = 0x00000400, // Class - Interface = 0x00000800, // Interface - Reference = 0x00001000, // Generic type reference - Tuple = 0x00002000, // Tuple - Union = 0x00004000, // Union - Anonymous = 0x00008000, // Anonymous - FromSignature = 0x00010000, // Created for signature assignment check - ObjectLiteral = 0x00020000, // Originates in an object literal - ContainsUndefined = 0x00040000, // Type is or contains Undefined or Null type - ContainsLiteral = 0x00080000, // Type is or contains object literal type + Any = 0x00000001, + String = 0x00000002, + Number = 0x00000004, + Boolean = 0x00000008, + Void = 0x00000010, + Undefined = 0x00000020, + Null = 0x00000040, + Enum = 0x00000080, // Enum type + StringLiteral = 0x00000100, // String literal type + TypeParameter = 0x00000200, // Type parameter + Class = 0x00000400, // Class + Interface = 0x00000800, // Interface + Reference = 0x00001000, // Generic type reference + Tuple = 0x00002000, // Tuple + Union = 0x00004000, // Union + Anonymous = 0x00008000, // Anonymous + FromSignature = 0x00010000, // Created for signature assignment check + ObjectLiteral = 0x00020000, // Originates in an object literal + ContainsUndefinedOrNull = 0x00040000, // Type is or contains Undefined or Null type + ContainsObjectLiteral = 0x00080000, // Type is or contains object literal type Intrinsic = Any | String | Number | Boolean | Void | Undefined | Null, StringLike = String | StringLiteral, NumberLike = Number | Enum, ObjectType = Class | Interface | Reference | Tuple | Anonymous, - RequiresWidening = ContainsUndefined | ContainsLiteral + RequiresWidening = ContainsUndefinedOrNull | ContainsObjectLiteral } // Properties common to all types