From 94a0daf2ea9261e22b8def1511034f14ca332a49 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 27 Sep 2016 16:03:12 -0700 Subject: [PATCH] Properly handle partially discriminated unions --- src/compiler/checker.ts | 66 +++++++++++++++++++++++------------------ src/compiler/types.ts | 3 +- 2 files changed, 39 insertions(+), 30 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 72d2ca92af0..638ff533e3e 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4366,7 +4366,7 @@ namespace ts { function getPropertiesOfUnionOrIntersectionType(type: UnionOrIntersectionType): Symbol[] { for (const current of type.types) { for (const prop of getPropertiesOfType(current)) { - getPropertyOfUnionOrIntersectionType(type, prop.name); + getUnionOrIntersectionProperty(type, prop.name); } // The properties of a union type are those that are present in all constituent types, so // we only need to check the properties of the first type @@ -4374,7 +4374,19 @@ namespace ts { break; } } - return type.resolvedProperties ? symbolsToArray(type.resolvedProperties) : emptyArray; + const props = type.resolvedProperties; + if (props) { + const result: Symbol[] = []; + for (const key in props) { + const prop = props[key]; + // We need to filter out partial properties in union types + if (!(prop.flags & SymbolFlags.SyntheticProperty && (prop).isPartial)) { + result.push(prop); + } + } + return result; + } + return emptyArray; } function getPropertiesOfType(type: Type): Symbol[] { @@ -4427,6 +4439,7 @@ namespace ts { // Flags we want to propagate to the result if they exist in all source symbols let commonFlags = (containingType.flags & TypeFlags.Intersection) ? SymbolFlags.Optional : SymbolFlags.None; let isReadonly = false; + let isPartial = false; for (const current of types) { const type = getApparentType(current); if (type !== unknownType) { @@ -4444,21 +4457,20 @@ namespace ts { } } else if (containingType.flags & TypeFlags.Union) { - // A union type requires the property to be present in all constituent types - return undefined; + isPartial = true; } } } if (!props) { return undefined; } - if (props.length === 1) { + if (props.length === 1 && !isPartial) { return props[0]; } const propTypes: Type[] = []; const declarations: Declaration[] = []; let commonType: Type = undefined; - let hasCommonType = true; + let hasNonUniformType = false; for (const prop of props) { if (prop.declarations) { addRange(declarations, prop.declarations); @@ -4468,25 +4480,26 @@ namespace ts { commonType = type; } else if (type !== commonType) { - hasCommonType = false; + hasNonUniformType = true; } - propTypes.push(getTypeOfSymbol(prop)); + propTypes.push(type); } - const result = createSymbol( - SymbolFlags.Property | - SymbolFlags.Transient | - SymbolFlags.SyntheticProperty | - commonFlags, - name); + const result = createSymbol(SymbolFlags.Property | SymbolFlags.Transient | SymbolFlags.SyntheticProperty | commonFlags, name); result.containingType = containingType; - result.hasCommonType = hasCommonType; + result.hasNonUniformType = hasNonUniformType; + result.isPartial = isPartial; result.declarations = declarations; result.isReadonly = isReadonly; result.type = containingType.flags & TypeFlags.Union ? getUnionType(propTypes) : getIntersectionType(propTypes); return result; } - function getPropertyOfUnionOrIntersectionType(type: UnionOrIntersectionType, name: string): Symbol { + // Return the symbol for a given property in a union or intersection type, or undefined if the property + // does not exist in any constituent type. Note that the returned property may only be present in some + // constituents, in which case the isPartial flag is set when the containing type is union type. We need + // these partial properties when identifying discriminant properties, but otherwise they are filtered out + // and do not appear to be present in the union type. + function getUnionOrIntersectionProperty(type: UnionOrIntersectionType, name: string): Symbol { const properties = type.resolvedProperties || (type.resolvedProperties = createMap()); let property = properties[name]; if (!property) { @@ -4498,6 +4511,12 @@ namespace ts { return property; } + function getPropertyOfUnionOrIntersectionType(type: UnionOrIntersectionType, name: string): Symbol { + const property = getUnionOrIntersectionProperty(type, name); + // We need to filter out partial properties in union types + return property && !(property.flags & SymbolFlags.SyntheticProperty && (property).isPartial) ? property : undefined; + } + /** * Return the symbol for the property with the given name in the given type. Creates synthetic union properties when * necessary, maps primitive types and type parameters are to their apparent types, and augments with properties from @@ -8078,21 +8097,10 @@ namespace ts { function isDiscriminantProperty(type: Type, name: string) { if (type && type.flags & TypeFlags.Union) { - let prop = getPropertyOfType(type, name); - if (!prop) { - // The type may be a union that includes nullable or primitive types. If filtering - // those out produces a different type, get the property from that type instead. - // Effectively, we're checking if this *could* be a discriminant property once nullable - // and primitive types are removed by other type guards. - const filteredType = getTypeWithFacts(type, TypeFacts.Discriminatable); - if (filteredType !== type && filteredType.flags & TypeFlags.Union) { - prop = getPropertyOfType(filteredType, name); - } - } + const prop = getUnionOrIntersectionProperty(type, name); if (prop && prop.flags & SymbolFlags.SyntheticProperty) { if ((prop).isDiscriminantProperty === undefined) { - (prop).isDiscriminantProperty = !(prop).hasCommonType && - isLiteralType(getTypeOfSymbol(prop)); + (prop).isDiscriminantProperty = (prop).hasNonUniformType && isLiteralType(getTypeOfSymbol(prop)); } return (prop).isDiscriminantProperty; } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index af71846145f..ab48ae543c2 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2289,7 +2289,8 @@ namespace ts { mapper?: TypeMapper; // Type mapper for instantiation alias referenced?: boolean; // True if alias symbol has been referenced as a value containingType?: UnionOrIntersectionType; // Containing union or intersection type for synthetic property - hasCommonType?: boolean; // True if constituents of synthetic property all have same type + hasNonUniformType?: boolean; // True if constituents have non-uniform types + isPartial?: boolean; // True if syntheric property of union type occurs in some but not all constituents isDiscriminantProperty?: boolean; // True if discriminant synthetic property resolvedExports?: SymbolTable; // Resolved exports of module exportsChecked?: boolean; // True if exports of external module have been checked