Skip to content

Commit

Permalink
Remove deduplication logic now that subtype reduction was optimized
Browse files Browse the repository at this point in the history
  • Loading branch information
ahejlsberg committed Feb 28, 2021
1 parent d282c70 commit a18aac2
Showing 1 changed file with 1 addition and 44 deletions.
45 changes: 1 addition & 44 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25215,9 +25215,8 @@ namespace ts {
if (forceTuple || inConstContext || contextualType && forEachType(contextualType, isTupleLikeType)) {
return createArrayLiteralType(createTupleType(elementTypes, elementFlags, /*readonly*/ inConstContext));
}
const deduplicatedTypes = deduplicateObjectOrArrayLiteralTypes(sameMap(elementTypes, (t, i) => elementFlags[i] & ElementFlags.Variadic ? getIndexedAccessTypeOrUndefined(t, numberType) || anyType : t));
return createArrayLiteralType(createArrayType(elementTypes.length ?
getUnionType(deduplicatedTypes, UnionReduction.Subtype) :
getUnionType(sameMap(elementTypes, (t, i) => elementFlags[i] & ElementFlags.Variadic ? getIndexedAccessTypeOrUndefined(t, numberType) || anyType : t), UnionReduction.Subtype) :
strictNullChecks ? implicitNeverType : undefinedWideningType, inConstContext));
}

Expand All @@ -25233,48 +25232,6 @@ namespace ts {
return literalType;
}

/**
* Replaces all references to structurally equivalent object and array literal types in the given list
* with references to a single one of those types. The process is applied recursively to properties of
* object literals and elements of array literals.
*/
function deduplicateObjectOrArrayLiteralTypes(types: Type[]) {
if (!some(types, isObjectOrArrayLiteralType)) {
return types;
}
const typeMap = new Map<string, Type>();
return sameMap(types, getInternedType);

function getInternedType(type: Type): Type {
if (type.flags & TypeFlags.Union) {
const newTypes = sameMap((<UnionType>type).types, getInternedType);
return newTypes !== (<UnionType>type).types ? getUnionType(newTypes) : type;
}
const key = getLiteralTypeKey(type);
return key ? typeMap.get(key) || (typeMap.set(key, type), type) : type;
}

function getLiteralTypeKey(type: Type): string | undefined {
const objectFlags = getObjectFlags(type);
if (objectFlags & ObjectFlags.ObjectLiteral) {
const props = getPropertiesOfObjectType(type);
const propTypes = map(props, p => getInternedType(getTypeOfSymbol(p)));
const nameLengths = map(props, p => p.flags & SymbolFlags.Optional ? -(<string>p.escapedName).length : (<string>p.escapedName).length);
const nameStrings = map(props, p => <string>p.escapedName);
return `${getTypeListId(propTypes)}|${nameLengths.join(",")}|${nameStrings.join("")}`;
}
if (objectFlags & ObjectFlags.ArrayLiteral) {
if (isArrayType(type)) {
return `${isReadonlyArrayType(type) ? "R" : "A"}${getTypeId(getInternedType(getTypeArguments(type)[0]))}`;
}
if (isTupleType(type)) {
return `T${getTypeId(type.target)}|${getTypeListId(sameMap(getTypeArguments(type), getInternedType))}`;
}
}
return undefined;
}
}

function isNumericName(name: DeclarationName): boolean {
switch (name.kind) {
case SyntaxKind.ComputedPropertyName:
Expand Down

0 comments on commit a18aac2

Please sign in to comment.