Skip to content

Commit 2450c19

Browse files
committed
Make lower priority inferences when inference process is blocked
1 parent 8f02055 commit 2450c19

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/compiler/checker.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15459,6 +15459,7 @@ namespace ts {
1545915459
let bivariant = false;
1546015460
let propagationType: Type;
1546115461
let inferenceCount = 0;
15462+
let inferenceBlocked = false;
1546215463
let allowComplexConstraintInference = true;
1546315464
inferFromTypes(originalSource, originalTarget);
1546415465

@@ -15655,6 +15656,7 @@ namespace ts {
1565515656
if (source.flags & (TypeFlags.Object | TypeFlags.Intersection)) {
1565615657
const key = source.id + "," + target.id;
1565715658
if (visited && visited.get(key)) {
15659+
inferenceBlocked = true;
1565815660
return;
1565915661
}
1566015662
(visited || (visited = createMap<boolean>())).set(key, true);
@@ -15667,6 +15669,7 @@ namespace ts {
1566715669
const symbol = isNonConstructorObject ? target.symbol : undefined;
1566815670
if (symbol) {
1566915671
if (contains(symbolStack, symbol)) {
15672+
inferenceBlocked = true;
1567015673
return;
1567115674
}
1567215675
(symbolStack || (symbolStack = [])).push(symbol);
@@ -15755,6 +15758,8 @@ namespace ts {
1575515758
const sources = source.flags & TypeFlags.Union ? (<UnionType>source).types : [source];
1575615759
const matched = new Array<boolean>(sources.length);
1575715760
let typeVariableCount = 0;
15761+
const saveInferenceBlocked = inferenceBlocked;
15762+
inferenceBlocked = false;
1575815763
// First infer to types that are not naked type variables. For each source type we
1575915764
// track whether inferences were made from that particular type to some target.
1576015765
for (const t of target.types) {
@@ -15771,14 +15776,15 @@ namespace ts {
1577115776
}
1577215777
// If there are naked type variables in the target, create a union of the source types
1577315778
// from which no inferences have been made so far and infer from that union to each naked
15774-
// type variable. If there is more than one naked type variable, give lower priority to
15775-
// the inferences as they are less specific.
15779+
// type variable. If there is more than one naked type variable, or if inference was blocked
15780+
// (meaning we didn't explore the types fully), give lower priority to the inferences as
15781+
// they are less specific.
1577615782
if (typeVariableCount > 0) {
1577715783
const unmatched = flatMap(sources, (s, i) => matched[i] ? undefined : s);
1577815784
if (unmatched.length) {
1577915785
const s = getUnionType(unmatched);
1578015786
const savePriority = priority;
15781-
if (typeVariableCount > 1) {
15787+
if (typeVariableCount > 1 || inferenceBlocked) {
1578215788
priority |= InferencePriority.NakedTypeVariable;
1578315789
}
1578415790
for (const t of target.types) {
@@ -15789,6 +15795,7 @@ namespace ts {
1578915795
priority = savePriority;
1579015796
}
1579115797
}
15798+
inferenceBlocked = saveInferenceBlocked;
1579215799
}
1579315800

1579415801
function inferToMappedType(source: Type, target: MappedType, constraintType: Type): boolean {

0 commit comments

Comments
 (0)