Skip to content

Commit ca47ddb

Browse files
committed
Only infer to single naked type parameters in intersections
1 parent ff95909 commit ca47ddb

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/compiler/checker.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14773,22 +14773,29 @@ namespace ts {
1477314773
// We infer from types that are not naked type variables first so that inferences we
1477414774
// make from nested naked type variables and given slightly higher priority by virtue
1477514775
// of being first in the candidates array.
14776+
let typeVariableCount = 0;
1477614777
for (const t of (<UnionOrIntersectionType>target).types) {
14777-
if (!getInferenceInfoForType(t)) {
14778+
if (getInferenceInfoForType(t)) {
14779+
typeVariableCount++;
14780+
}
14781+
else {
1477814782
inferFromTypes(source, t);
1477914783
}
1478014784
}
1478114785
// Inferences directly to naked type variables are given lower priority as they are
1478214786
// less specific. For example, when inferring from Promise<string> to T | Promise<T>,
14783-
// we want to infer string for T, not Promise<string> | string.
14784-
const savePriority = priority;
14785-
priority |= InferencePriority.NakedTypeVariable;
14786-
for (const t of (<UnionOrIntersectionType>target).types) {
14787-
if (getInferenceInfoForType(t)) {
14788-
inferFromTypes(source, t);
14787+
// we want to infer string for T, not Promise<string> | string. For intersection types
14788+
// we only infer to single naked type variables.
14789+
if (target.flags & TypeFlags.Union ? typeVariableCount !== 0 : typeVariableCount === 1) {
14790+
const savePriority = priority;
14791+
priority |= InferencePriority.NakedTypeVariable;
14792+
for (const t of (<UnionOrIntersectionType>target).types) {
14793+
if (getInferenceInfoForType(t)) {
14794+
inferFromTypes(source, t);
14795+
}
1478914796
}
14797+
priority = savePriority;
1479014798
}
14791-
priority = savePriority;
1479214799
}
1479314800
else if (source.flags & TypeFlags.Union) {
1479414801
// Source is a union or intersection type, infer from each constituent type

0 commit comments

Comments
 (0)