Skip to content

Commit 7ec7f02

Browse files
committed
Instantiate contextual type using non-fixing mapper
1 parent adf760a commit 7ec7f02

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/compiler/checker.ts

+14-12
Original file line numberDiff line numberDiff line change
@@ -14470,7 +14470,7 @@ namespace ts {
1447014470
const templateType = getTemplateTypeFromMappedType(target);
1447114471
const inference = createInferenceInfo(typeParameter);
1447214472
inferTypes([inference], sourceType, templateType);
14473-
return getTypeFromInference(inference);
14473+
return getTypeFromInference(inference) || emptyObjectType;
1447414474
}
1447514475

1447614476
function* getUnmatchedProperties(source: Type, target: Type, requireOptionalProperties: boolean, matchDiscriminantProperties: boolean) {
@@ -14514,7 +14514,7 @@ namespace ts {
1451414514
function getTypeFromInference(inference: InferenceInfo) {
1451514515
return inference.candidates ? getUnionType(inference.candidates, UnionReduction.Subtype) :
1451614516
inference.contraCandidates ? getIntersectionType(inference.contraCandidates) :
14517-
emptyObjectType;
14517+
undefined;
1451814518
}
1451914519

1452014520
function inferTypes(inferences: InferenceInfo[], originalSource: Type, originalTarget: Type, priority: InferencePriority = 0, contravariant = false) {
@@ -15016,8 +15016,8 @@ namespace ts {
1501615016

1501715017
function getInferredType(context: InferenceContext, index: number): Type {
1501815018
const inference = context.inferences[index];
15019-
let inferredType = inference.inferredType;
15020-
if (!inferredType) {
15019+
if (!inference.inferredType) {
15020+
let inferredType: Type | undefined;
1502115021
const signature = context.signature;
1502215022
if (signature) {
1502315023
const inferredCovariantType = inference.candidates ? getCovariantInference(inference, signature) : undefined;
@@ -15048,27 +15048,24 @@ namespace ts {
1504815048
// parameter should be instantiated to the empty object type.
1504915049
inferredType = instantiateType(defaultType, combineTypeMappers(createBackreferenceMapper(context, index), context.nonFixingMapper));
1505015050
}
15051-
else {
15052-
inferredType = getDefaultTypeArgumentType(!!(context.flags & InferenceFlags.AnyDefault));
15053-
}
1505415051
}
1505515052
}
1505615053
else {
1505715054
inferredType = getTypeFromInference(inference);
1505815055
}
1505915056

15060-
inference.inferredType = inferredType;
15057+
inference.inferredType = inferredType || getDefaultTypeArgumentType(!!(context.flags & InferenceFlags.AnyDefault));
1506115058

1506215059
const constraint = getConstraintOfTypeParameter(inference.typeParameter);
1506315060
if (constraint) {
1506415061
const instantiatedConstraint = instantiateType(constraint, context.nonFixingMapper);
15065-
if (!context.compareTypes(inferredType, getTypeWithThisArgument(instantiatedConstraint, inferredType))) {
15062+
if (!inferredType || !context.compareTypes(inferredType, getTypeWithThisArgument(instantiatedConstraint, inferredType))) {
1506615063
inference.inferredType = inferredType = instantiatedConstraint;
1506715064
}
1506815065
}
1506915066
}
1507015067

15071-
return inferredType;
15068+
return inference.inferredType;
1507215069
}
1507315070

1507415071
function getDefaultTypeArgumentType(isInJavaScriptFile: boolean): Type {
@@ -18046,8 +18043,13 @@ namespace ts {
1804618043
function instantiateContextualType(contextualType: Type | undefined, node: Expression): Type | undefined {
1804718044
if (contextualType && maybeTypeOfKind(contextualType, TypeFlags.Instantiable)) {
1804818045
const inferenceContext = getInferenceContext(node);
18049-
if (inferenceContext && inferenceContext.returnMapper) {
18050-
return instantiateInstantiableTypes(contextualType, inferenceContext.returnMapper);
18046+
if (inferenceContext) {
18047+
if ((isFunctionExpressionOrArrowFunction(node) || isObjectLiteralMethod(node)) && isContextSensitive(node)) {
18048+
return instantiateInstantiableTypes(contextualType, inferenceContext.nonFixingMapper);
18049+
}
18050+
if (inferenceContext.returnMapper) {
18051+
return instantiateInstantiableTypes(contextualType, inferenceContext.returnMapper);
18052+
}
1805118053
}
1805218054
}
1805318055
return contextualType;

0 commit comments

Comments
 (0)