@@ -15632,6 +15632,16 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1563215632 );
1563315633 }
1563415634
15635+ function getImplementationSignature(signature: Signature) {
15636+ return signature.typeParameters ?
15637+ signature.implementationSignatureCache ||= createImplementationSignature(signature) :
15638+ signature;
15639+ }
15640+
15641+ function createImplementationSignature(signature: Signature) {
15642+ return signature.typeParameters ? instantiateSignature(signature, createTypeMapper([], [])) : signature;
15643+ }
15644+
1563515645 function getBaseSignature(signature: Signature) {
1563615646 const typeParameters = signature.typeParameters;
1563715647 if (typeParameters) {
@@ -26364,9 +26374,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2636426374 // inference error only occurs when there are *conflicting* candidates, i.e.
2636526375 // candidates with no common supertype.
2636626376 const defaultType = getDefaultFromTypeParameter(inference.typeParameter);
26367- // Instantiate the default type. Any forward reference to a type
26368- // parameter should be instantiated to the empty object type.
26369- inferredType = instantiateType(defaultType, mergeTypeMappers(createBackreferenceMapper(context, index), context.nonFixingMapper));
26377+ if (defaultType) {
26378+ // Instantiate the default type. Any forward reference to a type
26379+ // parameter should be instantiated to the empty object type.
26380+ inferredType = instantiateType(defaultType, mergeTypeMappers(createBackreferenceMapper(context, index), context.nonFixingMapper));
26381+ }
2637026382 }
2637126383 }
2637226384 else {
@@ -34867,7 +34879,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3486734879 }
3486834880
3486934881 for (let candidateIndex = 0; candidateIndex < candidates.length; candidateIndex++) {
34870- const candidate = candidates[candidateIndex];
34882+ let candidate = candidates[candidateIndex];
3487134883 if (!hasCorrectTypeArgumentArity(candidate, typeArguments) || !hasCorrectArity(node, args, candidate, signatureHelpTrailingComma)) {
3487234884 continue;
3487334885 }
@@ -34876,6 +34888,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3487634888 let inferenceContext: InferenceContext | undefined;
3487734889
3487834890 if (candidate.typeParameters) {
34891+ // If we are *inside the body of candidate*, we need to create a clone of `candidate` with differing type parameter identities,
34892+ // so our inference results for this call doesn't pollute expression types referencing the outer type parameter!
34893+ if (candidate.declaration && findAncestor(node, a => a === candidate.declaration)) {
34894+ candidate = getImplementationSignature(candidate);
34895+ }
3487934896 let typeArgumentTypes: readonly Type[] | undefined;
3488034897 if (some(typeArguments)) {
3488134898 typeArgumentTypes = checkTypeArguments(candidate, typeArguments, /*reportErrors*/ false);
@@ -34885,7 +34902,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3488534902 }
3488634903 }
3488734904 else {
34888- inferenceContext = createInferenceContext(candidate.typeParameters, candidate, /*flags*/ isInJSFile(node) ? InferenceFlags.AnyDefault : InferenceFlags.None);
34905+ inferenceContext = createInferenceContext(candidate.typeParameters! , candidate, /*flags*/ isInJSFile(node) ? InferenceFlags.AnyDefault : InferenceFlags.None);
3488934906 // The resulting type arguments are instantiated with the inference context mapper, as the inferred types may still contain references to the inference context's
3489034907 // type variables via contextual projection. These are kept generic until all inferences are locked in, so the dependencies expressed can pass constraint checks.
3489134908 typeArgumentTypes = instantiateTypes(inferTypeArguments(node, candidate, args, argCheckMode | CheckMode.SkipGenericFunctions, inferenceContext), inferenceContext.nonFixingMapper);
0 commit comments