Skip to content

Commit 0500065

Browse files
committed
Avoid inference for fully-supplied type arguments
1 parent 0b44a2c commit 0500065

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

src/compiler/checker.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9155,8 +9155,15 @@ namespace ts {
91559155
return constraint && maybeTypeOfKind(constraint, TypeFlags.Primitive | TypeFlags.Index);
91569156
}
91579157

9158+
function getSuppliedType(context: InferenceContext, index: number): Type | undefined {
9159+
if (context.suppliedTypes && index < context.suppliedTypes.length) {
9160+
return context.inferredTypes[index] = context.suppliedTypes[index];
9161+
}
9162+
return undefined;
9163+
}
9164+
91589165
function getInferredType(context: InferenceContext, index: number): Type {
9159-
let inferredType = context.inferredTypes[index];
9166+
let inferredType = context.inferredTypes[index] || getSuppliedType(context, index);
91609167
let inferenceSucceeded: boolean;
91619168
if (!inferredType) {
91629169
const inferences = getInferenceCandidates(context, index);
@@ -13410,6 +13417,7 @@ namespace ts {
1341013417
}
1341113418
}
1341213419

13420+
const numTypeArguments = typeArguments ? typeArguments.length : 0;
1341313421
const candidates = candidatesOutArray || [];
1341413422
// reorderCandidates fills up the candidates array directly
1341513423
reorderCandidates(signatures, candidates);
@@ -13580,28 +13588,26 @@ namespace ts {
1358013588
? createInferenceContext(originalCandidate, /*inferUnionTypes*/ false)
1358113589
: undefined;
1358213590

13583-
// fix each supplied type argument in the inference context
13584-
if (typeArguments) {
13585-
for (let i = 0; i < typeArguments.length; i++) {
13586-
inferenceContext.inferredTypes[i] = getTypeFromTypeNode(typeArguments[i]);
13587-
inferenceContext.inferences[i].isFixed = true;
13588-
}
13589-
}
13590-
1359113591
while (true) {
1359213592
candidate = originalCandidate;
1359313593
if (candidate.typeParameters) {
13594-
// Check any supplied type arguments against the candidate.
13595-
typeArgumentsAreValid = !typeArguments || checkTypeArguments(candidate, typeArguments, inferenceContext.inferredTypes, /*reportErrors*/ false);
13596-
if (typeArgumentsAreValid) {
13594+
let typeArgumentTypes: Type[] | undefined;
13595+
if (typeArguments) {
13596+
// Check any supplied type arguments against the candidate.
13597+
typeArgumentTypes = map(typeArguments, getTypeFromTypeNode)
13598+
typeArgumentsAreValid = checkTypeArguments(candidate, typeArguments, typeArgumentTypes, /*reportErrors*/ false);
13599+
}
13600+
if ((!typeArguments || typeArgumentsAreValid) && numTypeArguments < candidate.typeParameters.length) {
1359713601
// Infer any unsupplied type arguments for the candidate.
13602+
inferenceContext.suppliedTypes = typeArgumentTypes;
1359813603
inferTypeArguments(node, candidate, args, excludeArgument, inferenceContext);
1359913604
typeArgumentsAreValid = inferenceContext.failedTypeParameterIndex === undefined;
13605+
typeArgumentTypes = inferenceContext.inferredTypes;
1360013606
}
1360113607
if (!typeArgumentsAreValid) {
1360213608
break;
1360313609
}
13604-
candidate = getSignatureInstantiation(candidate, inferenceContext.inferredTypes);
13610+
candidate = getSignatureInstantiation(candidate, typeArgumentTypes);
1360513611
}
1360613612
if (!checkApplicableSignature(node, args, candidate, relation, excludeArgument, /*reportErrors*/ false)) {
1360713613
break;

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3095,6 +3095,7 @@ namespace ts {
30953095
inferUnionTypes: boolean; // Infer union types for disjoint candidates (otherwise undefinedType)
30963096
inferences: TypeInferences[]; // Inferences made for each type parameter
30973097
inferredTypes: Type[]; // Inferred type for each type parameter
3098+
suppliedTypes?: Type[]; // Supplied types for non-default type parameters
30983099
mapper?: TypeMapper; // Type mapper for this inference context
30993100
failedTypeParameterIndex?: number; // Index of type parameter for which inference failed
31003101
// It is optional because in contextual signature instantiation, nothing fails

0 commit comments

Comments
 (0)