@@ -20401,6 +20401,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2040120401 return createTypeMapper(map(forwardInferences, i => i.typeParameter), map(forwardInferences, () => unknownType));
2040220402 }
2040320403
20404+ /**
20405+ * Return a type mapper that combines the context's return mapper with a mapper that erases any additional type parameters
20406+ * to their constraints.
20407+ * */
20408+ function createOuterReturnMapper(context: InferenceContext) {
20409+ return mergeTypeMappers(context.returnMapper, createInferenceContext(map(context.inferences, i => i.typeParameter), context.signature, context.flags, context.compareTypes).mapper);
20410+ }
20411+
2040420412 function combineTypeMappers(mapper1: TypeMapper | undefined, mapper2: TypeMapper): TypeMapper {
2040520413 return mapper1 ? makeCompositeTypeMapper(TypeMapKind.Composite, mapper1, mapper2) : mapper2;
2040620414 }
@@ -35541,8 +35549,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3554135549 // from the return type. We need a separate inference pass here because (a) instantiation of
3554235550 // the source type uses the outer context's return mapper (which excludes inferences made from
3554335551 // outer arguments), and (b) we don't want any further inferences going into this context.
35552+ // We use `createOuterReturnMapper` to ensure that all occurrences of outer type parameters are
35553+ // replaced with either inferences produced from the outer return type or constraints of those
35554+ // type parameters. This protects against circular inferences, i.e. avoiding situations where
35555+ // inferences reference type parameters for which the inferences are being made.
3554435556 const returnContext = createInferenceContext(signature.typeParameters!, signature, context.flags);
35545- const returnSourceType = instantiateType(contextualType, outerContext && outerContext.returnMapper );
35557+ const returnSourceType = instantiateType(contextualType, outerContext && createOuterReturnMapper( outerContext) );
3554635558 inferTypes(returnContext.inferences, returnSourceType, inferenceTargetType);
3554735559 context.returnMapper = some(returnContext.inferences, hasInferenceCandidates) ? getMapperFromContext(cloneInferredPartOfContext(returnContext)) : undefined;
3554835560 }
0 commit comments