Remove string literals from unions with matching template literals (#41276)

* Remove string literals from unions with matching template literals

* Add tests

* Accept new baselines
This commit is contained in:
Anders Hejlsberg
2020-10-27 16:21:07 -07:00
committed by GitHub
parent 71cd5d522d
commit 40b81224f9
6 changed files with 84 additions and 0 deletions

View File

@@ -13108,6 +13108,20 @@ namespace ts {
}
}
function removeStringLiteralsMatchedByTemplateLiterals(types: Type[]) {
const templates = filter(types, isPatternLiteralType);
if (templates.length) {
let i = types.length;
while (i > 0) {
i--;
const t = types[i];
if (t.flags & TypeFlags.StringLiteral && some(templates, template => isTypeSubtypeOf(t, template))) {
orderedRemoveItemAt(types, i);
}
}
}
}
// We sort and deduplicate the constituent types based on object identity. If the subtypeReduction
// flag is specified we also reduce the constituent type set to only include types that aren't subtypes
// of other types. Subtype reduction is expensive for large union types and is possible only when union
@@ -13133,6 +13147,9 @@ namespace ts {
if (includes & (TypeFlags.Literal | TypeFlags.UniqueESSymbol)) {
removeRedundantLiteralTypes(typeSet, includes);
}
if (includes & TypeFlags.StringLiteral && includes & TypeFlags.TemplateLiteral) {
removeStringLiteralsMatchedByTemplateLiterals(typeSet);
}
break;
case UnionReduction.Subtype:
if (!removeSubtypes(typeSet, !(includes & TypeFlags.IncludesStructuredOrInstantiable))) {