@@ -15500,15 +15500,10 @@ namespace ts {
15500
15500
// of all their possible values.
15501
15501
let matchingTypes: Type[] | undefined;
15502
15502
for (const t of (<UnionOrIntersectionType>source).types) {
15503
- if (typeIdenticalToSomeType(t, (<UnionOrIntersectionType>target).types)) {
15504
- (matchingTypes || (matchingTypes = [])).push(t);
15505
- inferFromTypes(t, t);
15506
- }
15507
- else if (t.flags & (TypeFlags.NumberLiteral | TypeFlags.StringLiteral)) {
15508
- const b = getBaseTypeOfLiteralType(t);
15509
- if (typeIdenticalToSomeType(b, (<UnionOrIntersectionType>target).types)) {
15510
- (matchingTypes || (matchingTypes = [])).push(t, b);
15511
- }
15503
+ const matched = findMatchedType(t, <UnionOrIntersectionType>target);
15504
+ if (matched) {
15505
+ (matchingTypes || (matchingTypes = [])).push(matched);
15506
+ inferFromTypes(matched, matched);
15512
15507
}
15513
15508
}
15514
15509
// Next, to improve the quality of inferences, reduce the source and target types by
@@ -15519,6 +15514,14 @@ namespace ts {
15519
15514
target = removeTypesFromUnionOrIntersection(<UnionOrIntersectionType>target, matchingTypes);
15520
15515
}
15521
15516
}
15517
+ else if (target.flags & TypeFlags.Union && !(target.flags & TypeFlags.EnumLiteral) || target.flags & TypeFlags.Intersection) {
15518
+ const matched = findMatchedType(source, <UnionOrIntersectionType>target);
15519
+ if (matched) {
15520
+ inferFromTypes(matched, matched);
15521
+ source = target.flags & TypeFlags.Union ? neverType : unknownType;
15522
+ target = removeTypesFromUnionOrIntersection(<UnionOrIntersectionType>target, [matched]);
15523
+ }
15524
+ }
15522
15525
else if (target.flags & (TypeFlags.IndexedAccess | TypeFlags.Substitution)) {
15523
15526
target = getActualTypeVariable(target);
15524
15527
}
@@ -15955,6 +15958,19 @@ namespace ts {
15955
15958
return false;
15956
15959
}
15957
15960
15961
+ function findMatchedType(type: Type, target: UnionOrIntersectionType) {
15962
+ if (typeIdenticalToSomeType(type, target.types)) {
15963
+ return type;
15964
+ }
15965
+ if (type.flags & (TypeFlags.NumberLiteral | TypeFlags.StringLiteral) && target.flags & TypeFlags.Union) {
15966
+ const base = getBaseTypeOfLiteralType(type);
15967
+ if (typeIdenticalToSomeType(base, target.types)) {
15968
+ return base;
15969
+ }
15970
+ }
15971
+ return undefined;
15972
+ }
15973
+
15958
15974
/**
15959
15975
* Return a new union or intersection type computed by removing a given set of types
15960
15976
* from a given union or intersection type.
0 commit comments