@@ -10325,6 +10325,19 @@ namespace ts {
10325
10325
}
10326
10326
}
10327
10327
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
+
10328
10341
function inferTypes(inferences: InferenceInfo[], originalSource: Type, originalTarget: Type, priority: InferencePriority = 0) {
10329
10342
let symbolStack: Symbol[];
10330
10343
let visited: Map<boolean>;
@@ -10518,10 +10531,14 @@ namespace ts {
10518
10531
return;
10519
10532
}
10520
10533
}
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
+ }
10525
10542
}
10526
10543
10527
10544
function inferFromProperties(source: Type, target: Type) {
0 commit comments