Skip to content

Commit f93f8d3

Browse files
committed
Tweak union type inference to restore previous behavior
1 parent 17cedda commit f93f8d3

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/compiler/checker.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14724,17 +14724,25 @@ namespace ts {
1472414724
inferFromTypes(source, (<ConditionalType>target).falseType);
1472514725
}
1472614726
else if (target.flags & TypeFlags.UnionOrIntersection) {
14727+
// We infer from types that are not naked type variables first so that inferences we
14728+
// make from nested naked type variables and given slightly higher priority by virtue
14729+
// of being first in the candidates array.
14730+
for (const t of (<UnionOrIntersectionType>target).types) {
14731+
if (!getInferenceInfoForType(t)) {
14732+
inferFromTypes(source, t);
14733+
}
14734+
}
14735+
// Inferences directly to naked type variables are given lower priority as they are
14736+
// less specific. For example, when inferring from Promise<string> to T | Promise<T>,
14737+
// we want to infer string for T, not Promise<string> | string.
14738+
const savePriority = priority;
14739+
priority |= InferencePriority.NakedTypeVariable;
1472714740
for (const t of (<UnionOrIntersectionType>target).types) {
14728-
const savePriority = priority;
14729-
// Inferences directly to naked type variables are given lower priority as they are
14730-
// less specific. For example, when inferring from Promise<string> to T | Promise<T>,
14731-
// we want to infer string for T, not Promise<string> | string.
1473214741
if (getInferenceInfoForType(t)) {
14733-
priority |= InferencePriority.NakedTypeVariable;
14742+
inferFromTypes(source, t);
1473414743
}
14735-
inferFromTypes(source, t);
14736-
priority = savePriority;
1473714744
}
14745+
priority = savePriority;
1473814746
}
1473914747
else if (source.flags & TypeFlags.Union) {
1474014748
// Source is a union or intersection type, infer from each constituent type

0 commit comments

Comments
 (0)