mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-26 00:36:29 -05:00
Merge pull request #26425 from mattmccutchen/issue-26405
Go back to old narrowing algorithm but don't narrow in cases like #26130
This commit is contained in:
@@ -13900,6 +13900,18 @@ namespace ts {
|
||||
return flow.id;
|
||||
}
|
||||
|
||||
function typeMaybeAssignableTo(source: Type, target: Type) {
|
||||
if (!(source.flags & TypeFlags.Union)) {
|
||||
return isTypeAssignableTo(source, target);
|
||||
}
|
||||
for (const t of (<UnionType>source).types) {
|
||||
if (isTypeAssignableTo(t, target)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Remove those constituent types of declaredType to which no constituent type of assignedType is assignable.
|
||||
// For example, when a variable of type number | string | boolean is assigned a value of type number | boolean,
|
||||
// we remove type string.
|
||||
@@ -13908,8 +13920,12 @@ namespace ts {
|
||||
if (assignedType.flags & TypeFlags.Never) {
|
||||
return assignedType;
|
||||
}
|
||||
const reducedType = filterType(declaredType, t => isTypeComparableTo(assignedType, t));
|
||||
if (!(reducedType.flags & TypeFlags.Never)) {
|
||||
const reducedType = filterType(declaredType, t => typeMaybeAssignableTo(assignedType, t));
|
||||
// Our crude heuristic produces an invalid result in some cases: see GH#26130.
|
||||
// For now, when that happens, we give up and don't narrow at all. (This also
|
||||
// means we'll never narrow for erroneous assignments where the assigned type
|
||||
// is not assignable to the declared type.)
|
||||
if (isTypeAssignableTo(assignedType, reducedType)) {
|
||||
return reducedType;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user