-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix type when annotated with a JSDoc function type #22692
Fix type when annotated with a JSDoc function type #22692
Conversation
Previously, 1. A variable annotated with a JSDoc function type would not require all its parameters to be provided. This should only apply to functions without a type annotation. 2. A parameter in a function with a JSDoc function type annotation would still have the type 'any'. 3. Two `var` declarations in a Typescript and Javascript file, respectively, would error even when they had identical function types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't construct signatures need to be handled in the same way?
src/compiler/checker.ts
Outdated
const isUntypedSignatureInJSFile = !iife && | ||
!isJSConstructSignature && | ||
isInJavaScriptFile(declaration) && | ||
declaration.kind !== SyntaxKind.FunctionType && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ConstructorType
?
Yep, good catch. Also I forgot to update baselines. A few of them changed now that more types are available. |
@@ -6699,7 +6699,14 @@ namespace ts { | |||
let hasThisParameter: boolean; | |||
const iife = getImmediatelyInvokedFunctionExpression(declaration); | |||
const isJSConstructSignature = isJSDocConstructSignature(declaration); | |||
const isUntypedSignatureInJSFile = !iife && !isJSConstructSignature && isInJavaScriptFile(declaration) && !hasJSDocParameterTags(declaration); | |||
const isUntypedSignatureInJSFile = !iife && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What case is handling when the declaration is an object literal with call/construct signatures, (ie, {(): void}
)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
!getJSDocType(declaration)
. I'll add a test case. Note that the new code in getJSDocType
doesn't look deep enough to get type of the parameter out of the method declaration inside the object literal type.
src/compiler/checker.ts
Outdated
@@ -6699,7 +6699,14 @@ namespace ts { | |||
let hasThisParameter: boolean; | |||
const iife = getImmediatelyInvokedFunctionExpression(declaration); | |||
const isJSConstructSignature = isJSDocConstructSignature(declaration); | |||
const isUntypedSignatureInJSFile = !iife && !isJSConstructSignature && isInJavaScriptFile(declaration) && !hasJSDocParameterTags(declaration); | |||
const isUntypedSignatureInJSFile = !iife && | |||
!isJSConstructSignature && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
!isJSConstructSignature
is redundant if we test declaration.kind !== SyntaxKind.FunctionType
anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch. Removed.
src/compiler/utilities.ts
Outdated
if (typeTag && typeTag.typeExpression && isFunctionLike(typeTag.typeExpression.type)) { | ||
const i = node.parent.parameters.indexOf(node); | ||
if (i > -1) { | ||
return typeTag.typeExpression.type.parameters[i].type; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs a length check -- we don't know that typeTag.typeExpression.type
has the same # parameters as the actual function.
Instead of a syntactic check in getJSDocTag
return type; | ||
if (!isContextSensitiveFunctionOrObjectLiteralMethod(func)) { | ||
return undefined; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is just an inversion of the guard and a subsequent un-indent.
@weswigham I moved the change from |
!!! error TS2322: Types of parameters 'num' and 'arg0' are incompatible. | ||
!!! error TS2322: Type 'number' is not assignable to type 'string | undefined'. | ||
~~~~~~~ | ||
!!! error TS2322: Type '"0"' is not assignable to type 'number'. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you know why we infer a literal type here now? AFAIK we only generate a literal if the contextual type of the literal is the same domain as the literal itself, which would imply that the contextual type here is string
, which seems wrong, given the annotation above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The inferred type of "0" is for the initialiser, which was already of type "0" (see the type baselines below). You get the same error in Typescript with an type annotation, too:
function f(x: number = "hi") {
}
Gives the error "Type "hi" is not assignable to type 'number'." This error is expected now that num
is of type number
from contextual typing. Previously the arrow was typed with no contextual type, then assigned to arrowFunc
and checked against its type annotation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm. OK.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not great, but it's consistent with the rest of the the compiler. Note that our baselines have gone back and forth on this in the last year or two.
src/compiler/checker.ts
Outdated
const isUntypedSignatureInJSFile = !iife && !isJSConstructSignature && isInJavaScriptFile(declaration) && !hasJSDocParameterTags(declaration); | ||
const isUntypedSignatureInJSFile = !iife && | ||
isInJavaScriptFile(declaration) && | ||
declaration.kind !== SyntaxKind.FunctionType && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than skipping (some of the) the type-space signature declaration kinds, can we just check if it is a concrete declaration kind? That is the point here, right; to exclude type-space declaration kinds?
export type ConcreteSignatureDeclaration =
| FunctionDeclaration
| MethodDeclaration
| ConstructorDeclaration
| AccessorDeclaration
| FunctionExpression
| ArrowFunction;
export function isConcreteSignatureDeclaration(declaration: Node): node is ConcreteSignatureDeclaration {
return isFunctionExpression(declaration) || isArrowFunction(declaration) || isMethodOrAccessor(declaration) || isFunctionDeclaration(declaration) || isConstructorDeclaration(declaration);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, a type signature is by definition not untyped, even if it's in a JS file. I used your code in the new commit.
Instead of excluding type signatures piecemeal.
@sandersn Not sure how your point releases work exactly, but any chance of this being pulled into 2.8.2 or whatever is next? It appears it didn't make it into 2.8.1 and it unlocks a lot of new type checking/inference goodness via jsdocs :) |
Chances are not good. We keep point releases down to fixing regressions from the major release, and the bugs fixed in this PR have been around for ages. We have a major release every 6-8 weeks, so 2.9 should at least have a release candidate by then. :) |
Got it, no problem. Thanks! And thanks for all the work getting these working. |
Previously,
var
declarations in a Typescript and Javascript file, respectively, would error even when they had identical function types.Fixes #22080
I don't think there are bugs filed for (2) or (3).