@@ -11472,7 +11472,8 @@ namespace ts {
11472
11472
findMatchingDiscriminantType(source, target) ||
11473
11473
findMatchingTypeReferenceOrTypeAliasReference(source, target) ||
11474
11474
findBestTypeForObjectLiteral(source, target) ||
11475
- findBestTypeForInvokable(source, target);
11475
+ findBestTypeForInvokable(source, target) ||
11476
+ findMostOverlappyType(source, target);
11476
11477
11477
11478
isRelatedTo(source, bestMatchingType || targetTypes[targetTypes.length - 1], /*reportErrors*/ true);
11478
11479
}
@@ -11512,6 +11513,32 @@ namespace ts {
11512
11513
}
11513
11514
}
11514
11515
11516
+ function findMostOverlappyType(source: Type, unionTarget: UnionOrIntersectionType) {
11517
+ if (!(source.flags & (TypeFlags.Object | TypeFlags.Intersection))) {
11518
+ return undefined;
11519
+ }
11520
+ const sourceProperties = getPropertiesOfType(source);
11521
+ let bestType;
11522
+ let count = -1;
11523
+ for (const target of unionTarget.types) {
11524
+ if (!(target.flags & (TypeFlags.Object | TypeFlags.Intersection))) {
11525
+ continue;
11526
+ }
11527
+
11528
+ let currentCount = 0;
11529
+ for (const prop of sourceProperties) {
11530
+ if (getPropertyOfType(target, prop.escapedName)) {
11531
+ currentCount++;
11532
+ }
11533
+ }
11534
+ if (currentCount >= count) {
11535
+ count = currentCount;
11536
+ bestType = target;
11537
+ }
11538
+ }
11539
+ return bestType;
11540
+ }
11541
+
11515
11542
// Keep this up-to-date with the same logic within `getApparentTypeOfContextualType`, since they should behave similarly
11516
11543
function findMatchingDiscriminantType(source: Type, target: UnionOrIntersectionType) {
11517
11544
let match: Type | undefined;
0 commit comments