Skip to content

Commit 13750d2

Browse files
committed
Only infer from members of object types if the types are possibly related
1 parent c06a30a commit 13750d2

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/compiler/checker.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10325,6 +10325,19 @@ namespace ts {
1032510325
}
1032610326
}
1032710327

10328+
function isPossiblyAssignableTo(source: Type, target: Type) {
10329+
const properties = getPropertiesOfObjectType(target);
10330+
for (const targetProp of properties) {
10331+
if (!(targetProp.flags & (SymbolFlags.Optional | SymbolFlags.Prototype))) {
10332+
const sourceProp = getPropertyOfObjectType(source, targetProp.escapedName);
10333+
if (!sourceProp) {
10334+
return false;
10335+
}
10336+
}
10337+
}
10338+
return true;
10339+
}
10340+
1032810341
function inferTypes(inferences: InferenceInfo[], originalSource: Type, originalTarget: Type, priority: InferencePriority = 0) {
1032910342
let symbolStack: Symbol[];
1033010343
let visited: Map<boolean>;
@@ -10518,10 +10531,14 @@ namespace ts {
1051810531
return;
1051910532
}
1052010533
}
10521-
inferFromProperties(source, target);
10522-
inferFromSignatures(source, target, SignatureKind.Call);
10523-
inferFromSignatures(source, target, SignatureKind.Construct);
10524-
inferFromIndexTypes(source, target);
10534+
// Infer from the members of source and target only if the two types are possibly related. We check
10535+
// in both directions because we may be inferring for a co-variant or a contra-variant position.
10536+
if (isPossiblyAssignableTo(source, target) || isPossiblyAssignableTo(target, source)) {
10537+
inferFromProperties(source, target);
10538+
inferFromSignatures(source, target, SignatureKind.Call);
10539+
inferFromSignatures(source, target, SignatureKind.Construct);
10540+
inferFromIndexTypes(source, target);
10541+
}
1052510542
}
1052610543

1052710544
function inferFromProperties(source: Type, target: Type) {

0 commit comments

Comments
 (0)