Skip to content

Commit bf78470

Browse files
committed
Fix overly aggressive optimization
1 parent 6d25a42 commit bf78470

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/compiler/checker.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7242,17 +7242,17 @@ namespace ts {
72427242
return false;
72437243
}
72447244

7245-
// Remove those constituent types of currentType to which no constituent type of assignedType is assignable.
7245+
// Remove those constituent types of declaredType to which no constituent type of assignedType is assignable.
72467246
// For example, when a variable of type number | string | boolean is assigned a value of type number | boolean,
72477247
// we remove type string.
7248-
function getAssignmentReducedType(currentType: Type, assignedType: Type) {
7249-
if (currentType !== assignedType && currentType.flags & TypeFlags.Union) {
7250-
const reducedTypes = filter((<UnionType>currentType).types, t => typeMaybeAssignableTo(assignedType, t));
7248+
function getAssignmentReducedType(declaredType: Type, assignedType: Type) {
7249+
if (declaredType !== assignedType && declaredType.flags & TypeFlags.Union) {
7250+
const reducedTypes = filter((<UnionType>declaredType).types, t => typeMaybeAssignableTo(assignedType, t));
72517251
if (reducedTypes.length) {
72527252
return reducedTypes.length === 1 ? reducedTypes[0] : getUnionType(reducedTypes);
72537253
}
72547254
}
7255-
return currentType;
7255+
return declaredType;
72567256
}
72577257

72587258
function getNarrowedTypeOfReference(type: Type, reference: Node) {
@@ -7389,10 +7389,11 @@ namespace ts {
73897389
for (const antecedent of flow.antecedents) {
73907390
const t = getTypeAtFlowNodeCached(antecedent);
73917391
if (t !== resolvingFlowType) {
7392-
// If the type at a particular antecedent path is the declared type, there is no
7393-
// reason to process more antecedents since the only possible outcome is subtypes
7394-
// that are be removed in the final union type anyway.
7395-
if (t === declaredType) {
7392+
// If the type at a particular antecedent path is the declared type and the
7393+
// reference is known to always be assigned (i.e. when declared and initial types
7394+
// are the same), there is no reason to process more antecedents since the only
7395+
// possible outcome is subtypes that will be removed in the final union type anyway.
7396+
if (t === declaredType && declaredType === initialType) {
73967397
return t;
73977398
}
73987399
if (!contains(antecedentTypes, t)) {

0 commit comments

Comments
 (0)