Merge pull request #26244 from Microsoft/fixThisAndContextualTypes

Revise logic that computes the contextual type for a parameter
This commit is contained in:
Anders Hejlsberg
2018-08-09 06:07:11 -07:00
committed by GitHub
2 changed files with 64 additions and 14 deletions

View File

@@ -15827,20 +15827,10 @@ namespace ts {
}
const contextualSignature = getContextualSignature(func);
if (contextualSignature) {
const funcHasRestParameter = hasRestParameter(func);
const len = func.parameters.length - (funcHasRestParameter ? 1 : 0);
let indexOfParameter = func.parameters.indexOf(parameter);
if (getThisParameter(func) !== undefined && !contextualSignature.thisParameter) {
Debug.assert(indexOfParameter !== 0); // Otherwise we should not have called `getContextuallyTypedParameterType`.
indexOfParameter -= 1;
}
if (indexOfParameter < len) {
return getTypeAtPosition(contextualSignature, indexOfParameter);
}
// If last parameter is contextually rest parameter get its type
if (funcHasRestParameter && indexOfParameter === len) {
return getRestTypeAtPosition(contextualSignature, indexOfParameter);
}
const index = func.parameters.indexOf(parameter) - (getThisParameter(func) ? 1 : 0);
return parameter.dotDotDotToken && lastOrUndefined(func.parameters) === parameter ?
getRestTypeAtPosition(contextualSignature, index) :
tryGetTypeAtPosition(contextualSignature, index);
}
}