Revert back to declared type when branches produce equivalent union

This commit is contained in:
Anders Hejlsberg committed 2021-01-03 18:01:05 -10:00
1 parent 8597325408
commit 5f7e1265c9
1 file changed
+14 -9
+14 -9
View File
@@ -21658,15 +21658,6 @@ namespace ts {
return hasEvolvingArrayType;
}
// At flow control branch or loop junctions, if the type along every antecedent code path
// is an evolving array type, we construct a combined evolving array type. Otherwise we
// finalize all evolving array types.
function getUnionOrEvolvingArrayType(types: Type[], subtypeReduction: UnionReduction) {
return isEvolvingArrayTypeList(types) ?
getEvolvingArrayType(getUnionType(map(types, getElementTypeOfEvolvingArrayType))) :
getUnionType(sameMap(types, finalizeEvolvingArrayType), subtypeReduction);
}
// Return true if the given node is 'x' in an 'x.length', x.push(value)', 'x.unshift(value)' or
// 'x[n] = value' operation, where 'n' is an expression of type any, undefined, or a number-like type.
function isEvolvingArrayOperationTarget(node: Node) {
@@ -22365,6 +22356,20 @@ namespace ts {
return result;
}
// At flow control branch or loop junctions, if the type along every antecedent code path
// is an evolving array type, we construct a combined evolving array type. Otherwise we
// finalize all evolving array types.
function getUnionOrEvolvingArrayType(types: Type[], subtypeReduction: UnionReduction) {
if (isEvolvingArrayTypeList(types)) {
return getEvolvingArrayType(getUnionType(map(types, getElementTypeOfEvolvingArrayType)));
}
const result = getUnionType(sameMap(types, finalizeEvolvingArrayType), subtypeReduction);
if (result !== declaredType && result.flags & declaredType.flags & TypeFlags.Union && arraysEqual((<UnionType>result).types, (<UnionType>declaredType).types)) {
return declaredType;
}
return result;
}
function isMatchingReferenceDiscriminant(expr: Expression, computedType: Type) {
const type = declaredType.flags & TypeFlags.Union ? declaredType : computedType;
if (!(type.flags & TypeFlags.Union) || !isAccessExpression(expr)) {