@@ -8038,7 +8038,7 @@ namespace ts {
8038
8038
const result = createSignature(signature.declaration, freshTypeParameters,
8039
8039
signature.thisParameter && instantiateSymbol(signature.thisParameter, mapper),
8040
8040
instantiateList(signature.parameters, mapper, instantiateSymbol),
8041
- instantiateType(signature. resolvedReturnType, mapper) ,
8041
+ /* resolvedReturnType*/ undefined ,
8042
8042
freshTypePredicate,
8043
8043
signature.minArgumentCount, signature.hasRestParameter, signature.hasLiteralTypes);
8044
8044
result.target = signature;
@@ -16347,37 +16347,40 @@ namespace ts {
16347
16347
return signature.parameters.length > 0 ? getTypeAtPosition(signature, 0) : neverType;
16348
16348
}
16349
16349
16350
- function assignContextualParameterTypes (signature: Signature, context: Signature, mapper: TypeMapper, checkMode: CheckMode ) {
16350
+ function inferFromAnnotatedParameters (signature: Signature, context: Signature, mapper: TypeMapper) {
16351
16351
const len = signature.parameters.length - (signature.hasRestParameter ? 1 : 0);
16352
- if (checkMode === CheckMode.Inferential) {
16353
- for (let i = 0; i < len; i++) {
16354
- const declaration = <ParameterDeclaration>signature.parameters[i].valueDeclaration;
16355
- if (declaration.type) {
16356
- inferTypes((<InferenceContext>mapper).inferences, getTypeFromTypeNode(declaration.type), getTypeAtPosition(context, i));
16357
- }
16352
+ for (let i = 0; i < len; i++) {
16353
+ const declaration = <ParameterDeclaration>signature.parameters[i].valueDeclaration;
16354
+ if (declaration.type) {
16355
+ inferTypes((<InferenceContext>mapper).inferences, getTypeFromTypeNode(declaration.type), getTypeAtPosition(context, i));
16358
16356
}
16359
16357
}
16358
+ }
16359
+
16360
+ function assignContextualParameterTypes(signature: Signature, context: Signature) {
16361
+ signature.typeParameters = context.typeParameters;
16360
16362
if (context.thisParameter) {
16361
16363
const parameter = signature.thisParameter;
16362
16364
if (!parameter || parameter.valueDeclaration && !(<ParameterDeclaration>parameter.valueDeclaration).type) {
16363
16365
if (!parameter) {
16364
16366
signature.thisParameter = createSymbolWithType(context.thisParameter, /*type*/ undefined);
16365
16367
}
16366
- assignTypeToParameterAndFixTypeParameters(signature.thisParameter, getTypeOfSymbol(context.thisParameter), mapper, checkMode );
16368
+ assignTypeToParameterAndFixTypeParameters(signature.thisParameter, getTypeOfSymbol(context.thisParameter));
16367
16369
}
16368
16370
}
16371
+ const len = signature.parameters.length - (signature.hasRestParameter ? 1 : 0);
16369
16372
for (let i = 0; i < len; i++) {
16370
16373
const parameter = signature.parameters[i];
16371
16374
if (!(<ParameterDeclaration>parameter.valueDeclaration).type) {
16372
16375
const contextualParameterType = getTypeAtPosition(context, i);
16373
- assignTypeToParameterAndFixTypeParameters(parameter, contextualParameterType, mapper, checkMode );
16376
+ assignTypeToParameterAndFixTypeParameters(parameter, contextualParameterType);
16374
16377
}
16375
16378
}
16376
16379
if (signature.hasRestParameter && isRestParameterIndex(context, signature.parameters.length - 1)) {
16377
16380
const parameter = lastOrUndefined(signature.parameters);
16378
16381
if (!(<ParameterDeclaration>parameter.valueDeclaration).type) {
16379
16382
const contextualParameterType = getTypeOfSymbol(lastOrUndefined(context.parameters));
16380
- assignTypeToParameterAndFixTypeParameters(parameter, contextualParameterType, mapper, checkMode );
16383
+ assignTypeToParameterAndFixTypeParameters(parameter, contextualParameterType);
16381
16384
}
16382
16385
}
16383
16386
}
@@ -16397,10 +16400,10 @@ namespace ts {
16397
16400
}
16398
16401
}
16399
16402
16400
- function assignTypeToParameterAndFixTypeParameters(parameter: Symbol, contextualType: Type, mapper: TypeMapper, checkMode: CheckMode ) {
16403
+ function assignTypeToParameterAndFixTypeParameters(parameter: Symbol, contextualType: Type) {
16401
16404
const links = getSymbolLinks(parameter);
16402
16405
if (!links.type) {
16403
- links.type = instantiateType( contextualType, mapper) ;
16406
+ links.type = contextualType;
16404
16407
const name = getNameOfDeclaration(parameter.valueDeclaration);
16405
16408
// if inference didn't come up with anything but {}, fall back to the binding pattern if present.
16406
16409
if (links.type === emptyObjectType &&
@@ -16409,38 +16412,6 @@ namespace ts {
16409
16412
}
16410
16413
assignBindingElementTypes(<ParameterDeclaration>parameter.valueDeclaration);
16411
16414
}
16412
- else if (checkMode === CheckMode.Inferential) {
16413
- // Even if the parameter already has a type, it might be because it was given a type while
16414
- // processing the function as an argument to a prior signature during overload resolution.
16415
- // If this was the case, it may have caused some type parameters to be fixed. So here,
16416
- // we need to ensure that type parameters at the same positions get fixed again. This is
16417
- // done by calling instantiateType to attach the mapper to the contextualType, and then
16418
- // calling inferTypes to force a walk of contextualType so that all the correct fixing
16419
- // happens. The choice to pass in links.type may seem kind of arbitrary, but it serves
16420
- // to make sure that all the correct positions in contextualType are reached by the walk.
16421
- // Here is an example:
16422
- //
16423
- // interface Base {
16424
- // baseProp;
16425
- // }
16426
- // interface Derived extends Base {
16427
- // toBase(): Base;
16428
- // }
16429
- //
16430
- // var derived: Derived;
16431
- //
16432
- // declare function foo<T>(x: T, func: (p: T) => T): T;
16433
- // declare function foo<T>(x: T, func: (p: T) => T): T;
16434
- //
16435
- // var result = foo(derived, d => d.toBase());
16436
- //
16437
- // We are typing d while checking the second overload. But we've already given d
16438
- // a type (Derived) from the first overload. However, we still want to fix the
16439
- // T in the second overload so that we do not infer Base as a candidate for T
16440
- // (inferring Base would make type argument inference inconsistent between the two
16441
- // overloads).
16442
- inferTypes((<InferenceContext>mapper).inferences, links.type, instantiateType(contextualType, mapper));
16443
- }
16444
16415
}
16445
16416
16446
16417
function getReturnTypeFromJSDocComment(func: SignatureDeclaration | FunctionDeclaration): Type {
@@ -16739,7 +16710,13 @@ namespace ts {
16739
16710
if (contextualSignature) {
16740
16711
const signature = getSignaturesOfType(type, SignatureKind.Call)[0];
16741
16712
if (contextSensitive) {
16742
- assignContextualParameterTypes(signature, contextualSignature, getContextualMapper(node), checkMode);
16713
+ const contextualMapper = getContextualMapper(node);
16714
+ if (checkMode === CheckMode.Inferential) {
16715
+ inferFromAnnotatedParameters(signature, contextualSignature, contextualMapper);
16716
+ }
16717
+ const instantiatedContextualSignature = contextualMapper === identityMapper ?
16718
+ contextualSignature : instantiateSignature(contextualSignature, contextualMapper);
16719
+ assignContextualParameterTypes(signature, instantiatedContextualSignature);
16743
16720
}
16744
16721
if (mightFixTypeParameters || !node.type && !signature.resolvedReturnType) {
16745
16722
const returnType = getReturnTypeFromBody(node, checkMode);
0 commit comments