@@ -7918,27 +7918,14 @@ namespace ts {
7918
7918
return mapper;
7919
7919
}
7920
7920
7921
- function getInferenceMapper(context: InferenceContext): TypeMapper {
7922
- if (!context.mapper) {
7923
- const mapper: TypeMapper = t => {
7924
- const inferences = context.inferences;
7925
- for (let i = 0; i < inferences.length; i++) {
7926
- if (t === inferences[i].typeParameter) {
7927
- inferences[i].isFixed = true;
7928
- return getInferredType(context, i);
7929
- }
7930
- }
7931
- return t;
7932
- };
7933
- mapper.mappedTypes = context.signature.typeParameters;
7934
- mapper.context = context;
7935
- context.mapper = mapper;
7936
- }
7937
- return context.mapper;
7921
+ function isInferenceContext(mapper: TypeMapper): mapper is InferenceContext {
7922
+ return !!(<InferenceContext>mapper).signature;
7938
7923
}
7939
7924
7940
7925
function cloneTypeMapper(mapper: TypeMapper): TypeMapper {
7941
- return mapper && mapper.context ? getInferenceMapper(cloneInferenceContext(mapper.context)) : mapper;
7926
+ return mapper && isInferenceContext(mapper) ?
7927
+ createInferenceContext(mapper.callNode, mapper.signature, mapper.flags | InferenceFlags.NoDefault, mapper.inferences) :
7928
+ mapper;
7942
7929
}
7943
7930
7944
7931
function identityMapper(type: Type): Type {
@@ -10134,13 +10121,25 @@ namespace ts {
10134
10121
}
10135
10122
}
10136
10123
10137
- function createInferenceContext(callNode: CallLikeExpression, signature: Signature, flags: InferenceFlags): InferenceContext {
10138
- return {
10139
- callNode,
10140
- signature,
10141
- inferences: map(signature.typeParameters, createInferenceInfo),
10142
- flags,
10143
- };
10124
+ function createInferenceContext(callNode: CallLikeExpression, signature: Signature, flags: InferenceFlags, baseInferences?: InferenceInfo[]): InferenceContext {
10125
+ const inferences = baseInferences ? map(baseInferences, cloneInferenceInfo) : map(signature.typeParameters, createInferenceInfo);
10126
+ const context = mapper as InferenceContext;
10127
+ context.mappedTypes = signature.typeParameters;
10128
+ context.callNode = callNode;
10129
+ context.signature = signature;
10130
+ context.inferences = inferences;
10131
+ context.flags = flags;
10132
+ return context;
10133
+
10134
+ function mapper(t: Type): Type {
10135
+ for (let i = 0; i < inferences.length; i++) {
10136
+ if (t === inferences[i].typeParameter) {
10137
+ inferences[i].isFixed = true;
10138
+ return getInferredType(context, i);
10139
+ }
10140
+ }
10141
+ return t;
10142
+ }
10144
10143
}
10145
10144
10146
10145
function createInferenceInfo(typeParameter: TypeParameter): InferenceInfo {
@@ -10154,15 +10153,6 @@ namespace ts {
10154
10153
};
10155
10154
}
10156
10155
10157
- function cloneInferenceContext(context: InferenceContext): InferenceContext {
10158
- return {
10159
- callNode: context.callNode,
10160
- signature: context.signature,
10161
- inferences: map(context.inferences, cloneInferenceInfo),
10162
- flags: context.flags | InferenceFlags.NoDefault
10163
- };
10164
- }
10165
-
10166
10156
function cloneInferenceInfo(inference: InferenceInfo): InferenceInfo {
10167
10157
return {
10168
10158
typeParameter: inference.typeParameter,
@@ -10586,7 +10576,7 @@ namespace ts {
10586
10576
inferredType = instantiateType(defaultType,
10587
10577
combineTypeMappers(
10588
10578
createBackreferenceMapper(context.signature.typeParameters, index),
10589
- getInferenceMapper( context) ));
10579
+ context));
10590
10580
}
10591
10581
else {
10592
10582
inferredType = context.flags & InferenceFlags.AnyDefault ? anyType : emptyObjectType;
@@ -10600,7 +10590,7 @@ namespace ts {
10600
10590
if (inferenceSucceeded) {
10601
10591
const constraint = getConstraintOfTypeParameter(context.signature.typeParameters[index]);
10602
10592
if (constraint) {
10603
- const instantiatedConstraint = instantiateType(constraint, getInferenceMapper( context) );
10593
+ const instantiatedConstraint = instantiateType(constraint, context);
10604
10594
if (!isTypeAssignableTo(inferredType, getTypeWithThisArgument(instantiatedConstraint, inferredType))) {
10605
10595
inference.inferredType = inferredType = instantiatedConstraint;
10606
10596
}
@@ -14886,7 +14876,6 @@ namespace ts {
14886
14876
14887
14877
function inferTypeArguments(node: CallLikeExpression, signature: Signature, args: Expression[], excludeArgument: boolean[], context: InferenceContext): Type[] {
14888
14878
const inferences = context.inferences;
14889
- const inferenceMapper = getInferenceMapper(context);
14890
14879
14891
14880
// Clear out all the inference results from the last time inferTypeArguments was called on this context
14892
14881
for (let i = 0; i < inferences.length; i++) {
@@ -14932,7 +14921,7 @@ namespace ts {
14932
14921
if (argType === undefined) {
14933
14922
// For context sensitive arguments we pass the identityMapper, which is a signal to treat all
14934
14923
// context sensitive function expressions as wildcards
14935
- const mapper = excludeArgument && excludeArgument[i] !== undefined ? identityMapper : inferenceMapper ;
14924
+ const mapper = excludeArgument && excludeArgument[i] !== undefined ? identityMapper : context ;
14936
14925
argType = checkExpressionWithContextualType(arg, paramType, mapper);
14937
14926
}
14938
14927
@@ -14951,7 +14940,7 @@ namespace ts {
14951
14940
if (excludeArgument[i] === false) {
14952
14941
const arg = args[i];
14953
14942
const paramType = getTypeAtPosition(signature, i);
14954
- inferTypes(context.inferences, checkExpressionWithContextualType(arg, paramType, inferenceMapper ), paramType);
14943
+ inferTypes(context.inferences, checkExpressionWithContextualType(arg, paramType, context ), paramType);
14955
14944
}
14956
14945
}
14957
14946
}
@@ -16196,7 +16185,7 @@ namespace ts {
16196
16185
for (let i = 0; i < len; i++) {
16197
16186
const declaration = <ParameterDeclaration>signature.parameters[i].valueDeclaration;
16198
16187
if (declaration.type) {
16199
- inferTypes(mapper.context .inferences, getTypeFromTypeNode(declaration.type), getTypeAtPosition(context, i));
16188
+ inferTypes((<InferenceContext> mapper) .inferences, getTypeFromTypeNode(declaration.type), getTypeAtPosition(context, i));
16200
16189
}
16201
16190
}
16202
16191
}
@@ -16282,7 +16271,7 @@ namespace ts {
16282
16271
// T in the second overload so that we do not infer Base as a candidate for T
16283
16272
// (inferring Base would make type argument inference inconsistent between the two
16284
16273
// overloads).
16285
- inferTypes(mapper.context .inferences, links.type, instantiateType(contextualType, mapper));
16274
+ inferTypes((<InferenceContext> mapper) .inferences, links.type, instantiateType(contextualType, mapper));
16286
16275
}
16287
16276
}
16288
16277
0 commit comments