Skip to content

Commit 7d0dea0

Browse files
committed
Introduce and use createOuterReturnTypeMapper function
1 parent 16bc58f commit 7d0dea0

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

src/compiler/checker.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
}

src/compiler/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6492,7 +6492,7 @@ export const enum ObjectFlags {
64926492
CouldContainTypeVariablesComputed = 1 << 19, // CouldContainTypeVariables flag has been computed
64936493
/** @internal */
64946494
CouldContainTypeVariables = 1 << 20, // Type could contain a type variable
6495-
SingleSignatureType = 1 << 27,
6495+
SingleSignatureType = 1 << 27, // A single signature type extracted from a potentially broader type
64966496
ClassOrInterface = Class | Interface,
64976497
/** @internal */
64986498
RequiresWidening = ContainsWideningType | ContainsObjectOrArrayLiteral,

0 commit comments

Comments
 (0)