mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-06-11 20:37:46 -05:00
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:
@@ -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))) {
|
||||
|
||||
Reference in New Issue
Block a user