In JS, constructor functions infer from call+construct#28353
Conversation
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.
| @@ -414,27 +414,33 @@ namespace ts.codefix { | |||
| inferTypeFromContext(reference, checker, usageContext); | |||
| } | |||
| const isConstructor = declaration.kind === SyntaxKind.Constructor; | |||
There was a problem hiding this comment.
This is only used in one place, you could just inline to isConstructorDeclaration(declaration)
| return callContexts && declaration.parameters.map((parameter, parameterIndex): ParameterInference => { | ||
| const types: Type[] = []; | ||
| const isJS = isInJSFile(declaration); | ||
| const callContexts = isJS ? [...usageContext.constructContexts || [], ...usageContext.callContexts || []] : |
There was a problem hiding this comment.
You could just combine construct and call unconditionally?
There was a problem hiding this comment.
Welllll.....yeah, why not?
There was a problem hiding this comment.
Doesn't make a difference when run on the user test suite, and it simplifies the code considerably, so I think it's a good change.
| if (callContexts) { | ||
| for (const callContext of callContexts) { | ||
| if (callContext.argumentTypes.length <= parameterIndex) { | ||
| isOptional = isJS; |
There was a problem hiding this comment.
Similarly, does this need to depend on whether we're in JS?
There was a problem hiding this comment.
I thought about this one, but it's a bigger change -- right now the codefix only adds annotations, it doesn't add other punctuation around parameters. I'll open a bug to track this, though, because you are right, it should work (and it's probably not too hard).
| ////class TokenType { | ||
| //// label; | ||
| //// token; | ||
| //// constructor([|label, token? |]) { |
There was a problem hiding this comment.
If the user forgot type annotations they would probably forget ? -- I think this should work without it.
In JS, the inference code fix now infers from both calls and constructs of a function. I thought about only doing this for functions that the compiler thinks are constructor functions, but I'd rather err on the side of permissiveness for the codefix.
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.