Skip to content
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

Merged

Conversation

sandersn
Copy link
Member

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.

Fixes #22080

I don't think there are bugs filed for (2) or (3).

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.
Copy link
Member

@weswigham weswigham left a 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?

const isUntypedSignatureInJSFile = !iife &&
!isJSConstructSignature &&
isInJavaScriptFile(declaration) &&
declaration.kind !== SyntaxKind.FunctionType &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ConstructorType?

@sandersn
Copy link
Member Author

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 &&
Copy link
Member

@weswigham weswigham Mar 19, 2018

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})?

Copy link
Member Author

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.

@@ -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 &&
Copy link

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch. Removed.

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;
Copy link

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;
}
Copy link
Member Author

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.

@sandersn
Copy link
Member Author

@weswigham I moved the change from getJSDocType to getContextualSignature after our discussion this morning.

!!! 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'.
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. OK.

Copy link
Member Author

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.

const isUntypedSignatureInJSFile = !iife && !isJSConstructSignature && isInJavaScriptFile(declaration) && !hasJSDocParameterTags(declaration);
const isUntypedSignatureInJSFile = !iife &&
isInJavaScriptFile(declaration) &&
declaration.kind !== SyntaxKind.FunctionType &&
Copy link
Member

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);
}

Copy link
Member Author

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 sandersn merged commit b56093f into master Mar 19, 2018
@sandersn sandersn deleted the jsdoc-require-parameters-when-function-type-annotated branch March 19, 2018 23:00
@brendankenny
Copy link
Contributor

brendankenny commented Mar 27, 2018

@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 :)

@sandersn
Copy link
Member Author

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. :)

@brendankenny
Copy link
Contributor

Got it, no problem. Thanks! And thanks for all the work getting these working.

@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants