mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-30 22:32:33 -05:00
Add heuristic for extracting irreducible null and undefined types from intersections of unions (#33150)
This commit is contained in:
@@ -9996,6 +9996,16 @@ namespace ts {
|
||||
return true;
|
||||
}
|
||||
|
||||
function extractIrreducible(types: Type[], flag: TypeFlags) {
|
||||
if (every(types, t => !!(t.flags & TypeFlags.Union) && some((t as UnionType).types, tt => !!(tt.flags & flag)))) {
|
||||
for (let i = 0; i < types.length; i++) {
|
||||
types[i] = filterType(types[i], t => !(t.flags & flag));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// If the given list of types contains more than one union of primitive types, replace the
|
||||
// first with a union containing an intersection of those primitive types, then remove the
|
||||
// other unions and return true. Otherwise, do nothing and return false.
|
||||
@@ -10114,6 +10124,12 @@ namespace ts {
|
||||
// reduced we'll never reduce again, so this occurs at most once.
|
||||
result = getIntersectionType(typeSet, aliasSymbol, aliasTypeArguments);
|
||||
}
|
||||
else if (extractIrreducible(typeSet, TypeFlags.Undefined)) {
|
||||
result = getUnionType([getIntersectionType(typeSet), undefinedType], UnionReduction.Literal, aliasSymbol, aliasTypeArguments);
|
||||
}
|
||||
else if (extractIrreducible(typeSet, TypeFlags.Null)) {
|
||||
result = getUnionType([getIntersectionType(typeSet), nullType], UnionReduction.Literal, aliasSymbol, aliasTypeArguments);
|
||||
}
|
||||
else {
|
||||
// We are attempting to construct a type of the form X & (A | B) & Y. Transform this into a type of
|
||||
// the form X & A & Y | X & B & Y and recursively reduce until no union type constituents remain.
|
||||
|
||||
Reference in New Issue
Block a user