@@ -10778,14 +10778,8 @@ namespace ts {
10778
10778
return t => typeParameters.indexOf(t) >= index ? emptyObjectType : t;
10779
10779
}
10780
10780
10781
- function isInferenceContext(mapper: TypeMapper): mapper is InferenceContext {
10782
- return !!(<InferenceContext>mapper).typeParameters;
10783
- }
10784
-
10785
- function cloneTypeMapper(mapper: TypeMapper, extraFlags: InferenceFlags = 0): TypeMapper {
10786
- return mapper && isInferenceContext(mapper) ?
10787
- createInferenceContext(mapper.typeParameters, mapper.signature, mapper.flags | extraFlags, mapper.compareTypes, mapper.inferences) :
10788
- mapper;
10781
+ function cloneInferenceContext(context: InferenceContext, extraFlags: InferenceFlags = 0): InferenceContext {
10782
+ return createInferenceContext(context.typeParameters, context.signature, context.flags | extraFlags, context.compareTypes, context.inferences);
10789
10783
}
10790
10784
10791
10785
function cloneInferredPartOfContext(context: InferenceContext): InferenceContext | undefined {
@@ -11755,7 +11749,7 @@ namespace ts {
11755
11749
11756
11750
if (source.typeParameters && source.typeParameters !== target.typeParameters) {
11757
11751
target = getCanonicalSignature(target);
11758
- source = instantiateSignatureInContextOf(source, target, /*contextualMapper */ undefined, compareTypes);
11752
+ source = instantiateSignatureInContextOf(source, target, /*inferenceContext */ undefined, compareTypes);
11759
11753
}
11760
11754
11761
11755
const sourceCount = getParameterCount(source);
@@ -17531,7 +17525,7 @@ namespace ts {
17531
17525
while (type) {
17532
17526
const thisType = getThisTypeFromContextualType(type);
17533
17527
if (thisType) {
17534
- return instantiateType(thisType, getContextualMapper (containingLiteral));
17528
+ return instantiateType(thisType, getInferenceContext (containingLiteral));
17535
17529
}
17536
17530
if (literal.parent.kind !== SyntaxKind.PropertyAssignment) {
17537
17531
break;
@@ -18030,9 +18024,9 @@ namespace ts {
18030
18024
// return type inferences is available, instantiate those types using that mapper.
18031
18025
function instantiateContextualType(contextualType: Type | undefined, node: Expression): Type | undefined {
18032
18026
if (contextualType && maybeTypeOfKind(contextualType, TypeFlags.Instantiable)) {
18033
- const returnMapper = (<InferenceContext>getContextualMapper( node)).returnMapper ;
18034
- if (returnMapper) {
18035
- return instantiateInstantiableTypes(contextualType, returnMapper);
18027
+ const inferenceContext = getInferenceContext( node);
18028
+ if (inferenceContext && inferenceContext. returnMapper) {
18029
+ return instantiateInstantiableTypes(contextualType, inferenceContext. returnMapper);
18036
18030
}
18037
18031
}
18038
18032
return contextualType;
@@ -18134,9 +18128,9 @@ namespace ts {
18134
18128
return undefined;
18135
18129
}
18136
18130
18137
- function getContextualMapper (node: Node) {
18138
- const ancestor = findAncestor(node, n => !!n.contextualMapper );
18139
- return ancestor ? ancestor.contextualMapper! : identityMapper ;
18131
+ function getInferenceContext (node: Node) {
18132
+ const ancestor = findAncestor(node, n => !!n.inferenceContext );
18133
+ return ancestor && ancestor.inferenceContext! ;
18140
18134
}
18141
18135
18142
18136
function getContextualJsxElementAttributesType(node: JsxOpeningLikeElement) {
@@ -20135,19 +20129,19 @@ namespace ts {
20135
20129
}
20136
20130
20137
20131
// Instantiate a generic signature in the context of a non-generic signature (section 3.8.5 in TypeScript spec)
20138
- function instantiateSignatureInContextOf(signature: Signature, contextualSignature: Signature, contextualMapper ?: TypeMapper , compareTypes?: TypeComparer): Signature {
20132
+ function instantiateSignatureInContextOf(signature: Signature, contextualSignature: Signature, inferenceContext ?: InferenceContext , compareTypes?: TypeComparer): Signature {
20139
20133
const context = createInferenceContext(signature.typeParameters!, signature, InferenceFlags.None, compareTypes);
20140
- // We clone the contextualMapper to avoid fixing. For example, when the source signature is <T>(x: T) => T[] and
20134
+ // We clone the inferenceContext to avoid fixing. For example, when the source signature is <T>(x: T) => T[] and
20141
20135
// the contextual signature is (...args: A) => B, we want to infer the element type of A's constraint (say 'any')
20142
20136
// for T but leave it possible to later infer '[any]' back to A.
20143
20137
const restType = getEffectiveRestType(contextualSignature);
20144
- const mapper = contextualMapper && restType && restType.flags & TypeFlags.TypeParameter ? cloneTypeMapper(contextualMapper ) : contextualMapper ;
20138
+ const mapper = inferenceContext && restType && restType.flags & TypeFlags.TypeParameter ? cloneInferenceContext(inferenceContext ) : inferenceContext ;
20145
20139
const sourceSignature = mapper ? instantiateSignature(contextualSignature, mapper) : contextualSignature;
20146
20140
forEachMatchingParameterType(sourceSignature, signature, (source, target) => {
20147
20141
// Type parameters from outer context referenced by source type are fixed by instantiation of the source type
20148
20142
inferTypes(context.inferences, source, target);
20149
20143
});
20150
- if (!contextualMapper ) {
20144
+ if (!inferenceContext ) {
20151
20145
inferTypes(context.inferences, getReturnTypeOfSignature(contextualSignature), getReturnTypeOfSignature(signature), InferencePriority.ReturnType);
20152
20146
const signaturePredicate = getTypePredicateOfSignature(signature);
20153
20147
const contextualPredicate = getTypePredicateOfSignature(sourceSignature);
@@ -20192,7 +20186,8 @@ namespace ts {
20192
20186
// We clone the contextual mapper to avoid disturbing a resolution in progress for an
20193
20187
// outer call expression. Effectively we just want a snapshot of whatever has been
20194
20188
// inferred for any outer call expression so far.
20195
- const instantiatedType = instantiateType(contextualType, cloneTypeMapper(getContextualMapper(node), InferenceFlags.NoDefault));
20189
+ const outerContext = getInferenceContext(node);
20190
+ const instantiatedType = instantiateType(contextualType, outerContext && cloneInferenceContext(outerContext, InferenceFlags.NoDefault));
20196
20191
// If the contextual type is a generic function type with a single call signature, we
20197
20192
// instantiate the type with its own type parameters and type arguments. This ensures that
20198
20193
// the type parameters are not erased to type any during type inference such that they can
@@ -20326,7 +20321,7 @@ namespace ts {
20326
20321
// However "context" and "updater" are implicit and can't be specify by users. Only the first parameter, props,
20327
20322
// can be specified by users through attributes property.
20328
20323
const paramType = getEffectiveFirstArgumentForJsxSignature(signature, node);
20329
- const attributesType = checkExpressionWithContextualType(node.attributes, paramType, /*contextualMapper */ undefined, checkMode);
20324
+ const attributesType = checkExpressionWithContextualType(node.attributes, paramType, /*inferenceContext */ undefined, checkMode);
20330
20325
return checkTypeRelatedToAndOptionallyElaborate(attributesType, paramType, relation, reportErrors ? node.tagName : undefined, node.attributes);
20331
20326
}
20332
20327
@@ -20360,7 +20355,7 @@ namespace ts {
20360
20355
const arg = args[i];
20361
20356
if (arg.kind !== SyntaxKind.OmittedExpression) {
20362
20357
const paramType = getTypeAtPosition(signature, i);
20363
- const argType = checkExpressionWithContextualType(arg, paramType, /*contextualMapper */ undefined, checkMode);
20358
+ const argType = checkExpressionWithContextualType(arg, paramType, /*inferenceContext */ undefined, checkMode);
20364
20359
// If one or more arguments are still excluded (as indicated by CheckMode.SkipContextSensitive),
20365
20360
// we obtain the regular type of any object literal arguments because we may not have inferred complete
20366
20361
// parameter types yet and therefore excess property checks may yield false positives (see #17041).
@@ -21855,27 +21850,27 @@ namespace ts {
21855
21850
return signature.parameters.length > 0 ? getTypeAtPosition(signature, 0) : fallbackType;
21856
21851
}
21857
21852
21858
- function inferFromAnnotatedParameters(signature: Signature, context: Signature, mapper: TypeMapper ) {
21853
+ function inferFromAnnotatedParameters(signature: Signature, context: Signature, inferenceContext: InferenceContext ) {
21859
21854
const len = signature.parameters.length - (signature.hasRestParameter ? 1 : 0);
21860
21855
for (let i = 0; i < len; i++) {
21861
21856
const declaration = <ParameterDeclaration>signature.parameters[i].valueDeclaration;
21862
21857
if (declaration.type) {
21863
21858
const typeNode = getEffectiveTypeAnnotationNode(declaration);
21864
21859
if (typeNode) {
21865
- inferTypes((<InferenceContext>mapper) .inferences, getTypeFromTypeNode(typeNode), getTypeAtPosition(context, i));
21860
+ inferTypes(inferenceContext .inferences, getTypeFromTypeNode(typeNode), getTypeAtPosition(context, i));
21866
21861
}
21867
21862
}
21868
21863
}
21869
21864
const restType = getEffectiveRestType(context);
21870
21865
if (restType && restType.flags & TypeFlags.TypeParameter) {
21871
21866
// The contextual signature has a generic rest parameter. We first instantiate the contextual
21872
21867
// signature (without fixing type parameters) and assign types to contextually typed parameters.
21873
- const instantiatedContext = instantiateSignature(context, cloneTypeMapper(mapper ));
21868
+ const instantiatedContext = instantiateSignature(context, cloneInferenceContext(inferenceContext ));
21874
21869
assignContextualParameterTypes(signature, instantiatedContext);
21875
21870
// We then infer from a tuple type representing the parameters that correspond to the contextual
21876
21871
// rest parameter.
21877
21872
const restPos = getParameterCount(context) - 1;
21878
- inferTypes((<InferenceContext>mapper) .inferences, getRestTypeAtPosition(signature, restPos), restType);
21873
+ inferTypes(inferenceContext .inferences, getRestTypeAtPosition(signature, restPos), restType);
21879
21874
}
21880
21875
}
21881
21876
@@ -22316,12 +22311,12 @@ namespace ts {
22316
22311
if (contextualSignature) {
22317
22312
const signature = getSignaturesOfType(type, SignatureKind.Call)[0];
22318
22313
if (isContextSensitive(node)) {
22319
- const contextualMapper = getContextualMapper (node);
22314
+ const inferenceContext = getInferenceContext (node);
22320
22315
if (checkMode && checkMode & CheckMode.Inferential) {
22321
- inferFromAnnotatedParameters(signature, contextualSignature, contextualMapper );
22316
+ inferFromAnnotatedParameters(signature, contextualSignature, inferenceContext! );
22322
22317
}
22323
- const instantiatedContextualSignature = contextualMapper === identityMapper ?
22324
- contextualSignature : instantiateSignature(contextualSignature, contextualMapper) ;
22318
+ const instantiatedContextualSignature = inferenceContext ?
22319
+ instantiateSignature(contextualSignature, inferenceContext) : contextualSignature ;
22325
22320
assignContextualParameterTypes(signature, instantiatedContextualSignature);
22326
22321
}
22327
22322
if (!getReturnTypeFromAnnotation(node) && !signature.resolvedReturnType) {
@@ -23328,20 +23323,20 @@ namespace ts {
23328
23323
return node;
23329
23324
}
23330
23325
23331
- function checkExpressionWithContextualType(node: Expression, contextualType: Type, contextualMapper: TypeMapper | undefined, checkMode: CheckMode): Type {
23326
+ function checkExpressionWithContextualType(node: Expression, contextualType: Type, inferenceContext: InferenceContext | undefined, checkMode: CheckMode): Type {
23332
23327
const context = getContextNode(node);
23333
23328
const saveContextualType = context.contextualType;
23334
- const saveContextualMapper = context.contextualMapper ;
23329
+ const saveInferenceContext = context.inferenceContext ;
23335
23330
context.contextualType = contextualType;
23336
- context.contextualMapper = contextualMapper ;
23337
- const type = checkExpression(node, checkMode | CheckMode.Contextual | (contextualMapper ? CheckMode.Inferential : 0));
23331
+ context.inferenceContext = inferenceContext ;
23332
+ const type = checkExpression(node, checkMode | CheckMode.Contextual | (inferenceContext ? CheckMode.Inferential : 0));
23338
23333
// We strip literal freshness when an appropriate contextual type is present such that contextually typed
23339
23334
// literals always preserve their literal types (otherwise they might widen during type inference). An alternative
23340
23335
// here would be to not mark contextually typed literals as fresh in the first place.
23341
23336
const result = maybeTypeOfKind(type, TypeFlags.Literal) && isLiteralOfContextualType(type, instantiateContextualType(contextualType, node)) ?
23342
23337
getRegularTypeOfLiteralType(type) : type;
23343
23338
context.contextualType = saveContextualType;
23344
- context.contextualMapper = saveContextualMapper ;
23339
+ context.inferenceContext = saveInferenceContext ;
23345
23340
return result;
23346
23341
}
23347
23342
@@ -23467,7 +23462,7 @@ namespace ts {
23467
23462
if (contextualType) {
23468
23463
const contextualSignature = getSingleCallSignature(getNonNullableType(contextualType));
23469
23464
if (contextualSignature && !contextualSignature.typeParameters) {
23470
- const context = <InferenceContext>getContextualMapper (node);
23465
+ const context = <InferenceContext>getInferenceContext (node);
23471
23466
// We have an expression that is an argument of a generic function for which we are performing
23472
23467
// type argument inference. The expression is of a function type with a single generic call
23473
23468
// signature and a contextual function type with a single non-generic call signature. Now check
@@ -23507,7 +23502,7 @@ namespace ts {
23507
23502
if (checkMode & CheckMode.Inferential) {
23508
23503
// We have skipped a generic function during inferential typing. Obtain the inference context and
23509
23504
// indicate this has occurred such that we know a second pass of inference is be needed.
23510
- const context = <InferenceContext>getContextualMapper (node);
23505
+ const context = <InferenceContext>getInferenceContext (node);
23511
23506
context.flags |= InferenceFlags.SkippedGenericFunction;
23512
23507
}
23513
23508
}
0 commit comments