Skip to content

Commit 4f32691

Browse files
Try to report errors on types who have matching type references.
1 parent 9ba2eff commit 4f32691

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/compiler/checker.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10851,12 +10851,34 @@ namespace ts {
1085110851
}
1085210852
}
1085310853
if (reportErrors) {
10854-
const discriminantType = findMatchingDiscriminantType(source, target);
10855-
isRelatedTo(source, discriminantType || targetTypes[targetTypes.length - 1], /*reportErrors*/ true);
10854+
const bestMatchingType =
10855+
findMatchingDiscriminantType(source, target) ||
10856+
findMatchingTypeReferenceOrTypeAliasReference(source, target);
10857+
10858+
isRelatedTo(source, bestMatchingType || targetTypes[targetTypes.length - 1], /*reportErrors*/ true);
1085610859
}
1085710860
return Ternary.False;
1085810861
}
1085910862

10863+
function findMatchingTypeReferenceOrTypeAliasReference(source: Type, unionTarget: UnionOrIntersectionType) {
10864+
if (source.flags & TypeFlags.Object && (source as ObjectType).objectFlags & (ObjectFlags.Reference | ObjectFlags.Anonymous) && unionTarget.flags & TypeFlags.Union) {
10865+
return find(unionTarget.types, t => {
10866+
if (t.flags & TypeFlags.Object) {
10867+
if ((source as ObjectType).objectFlags & (t as ObjectType).objectFlags & ObjectFlags.Reference) {
10868+
return (source as TypeReference).target === (t as TypeReference).target;
10869+
}
10870+
if ((source as ObjectType).objectFlags & (t as ObjectType).objectFlags & ObjectFlags.Anonymous) {
10871+
// TODO (drosen): Not sure why the following isn't sufficient.
10872+
// return !!(source as AnonymousType).aliasSymbol && (source as AnonymousType).aliasSymbol === (target as AnonymousType).aliasSymbol;
10873+
return false;
10874+
}
10875+
}
10876+
return false;
10877+
});
10878+
}
10879+
}
10880+
10881+
1086010882
// Keep this up-to-date with the same logic within `getApparentTypeOfContextualType`, since they should behave similarly
1086110883
function findMatchingDiscriminantType(source: Type, target: UnionOrIntersectionType) {
1086210884
let match: Type | undefined;

0 commit comments

Comments
 (0)