In JS, constructor functions infer from call+construct (#28353)

* constructor functions infer from call+construct

Also fix an incorrect combining of inferences for rest parameters: the
inferred types will be arrays in the body of the function and the
arguments from outside the function will be the element type.

* All functions infer from call+construct contexts
This commit is contained in:
Nathan Shively-Sanders
2018-11-16 09:51:07 -08:00
committed by GitHub
parent 1089424035
commit ea8ccc2ce4
6 changed files with 67 additions and 6 deletions
+5 -5
View File
@@ -413,10 +413,9 @@ namespace ts.codefix {
cancellationToken.throwIfCancellationRequested();
inferTypeFromContext(reference, checker, usageContext);
}
const isConstructor = declaration.kind === SyntaxKind.Constructor;
const callContexts = isConstructor ? usageContext.constructContexts : usageContext.callContexts;
return callContexts && declaration.parameters.map((parameter, parameterIndex): ParameterInference => {
const types: Type[] = [];
const callContexts = [...usageContext.constructContexts || [], ...usageContext.callContexts || []];
return declaration.parameters.map((parameter, parameterIndex): ParameterInference => {
const types = [];
const isRest = isRestParameter(parameter);
let isOptional = false;
for (const callContext of callContexts) {
@@ -434,7 +433,8 @@ namespace ts.codefix {
}
}
if (isIdentifier(parameter.name)) {
types.push(...inferTypesFromReferences(getReferences(parameter.name, program, cancellationToken), checker, cancellationToken));
const inferred = inferTypesFromReferences(getReferences(parameter.name, program, cancellationToken), checker, cancellationToken);
types.push(...(isRest ? mapDefined(inferred, checker.getElementTypeOfArrayType) : inferred));
}
const type = unifyFromContext(types, checker);
return {