@@ -10851,12 +10851,34 @@ namespace ts {
10851
10851
}
10852
10852
}
10853
10853
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);
10856
10859
}
10857
10860
return Ternary.False;
10858
10861
}
10859
10862
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
+
10860
10882
// Keep this up-to-date with the same logic within `getApparentTypeOfContextualType`, since they should behave similarly
10861
10883
function findMatchingDiscriminantType(source: Type, target: UnionOrIntersectionType) {
10862
10884
let match: Type | undefined;
0 commit comments