Skip to content

JSDoc @type tag optional parameters #48132

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

Merged
merged 7 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8873,10 +8873,8 @@ namespace ts {
return getReturnTypeOfSignature(getterSignature);
}
}
if (isInJSFile(declaration)) {
const type = getParameterTypeOfTypeTag(func, declaration);
if (type) return type;
}
const parameterTypeOfTypeTag = getParameterTypeOfTypeTag(func, declaration);
if (parameterTypeOfTypeTag) return parameterTypeOfTypeTag;
// Use contextual parameter type if one is available
const type = declaration.symbol.escapedName === InternalSymbolName.This ? getContextualThisParameterType(func) : getContextuallyTypedParameterType(declaration);
if (type) {
Expand Down Expand Up @@ -12941,7 +12939,14 @@ namespace ts {
continue;
}
}
result.push(getSignatureFromDeclaration(decl));
// If this is a function or method declaration, get the signature from the @type tag for the sake of optional parameters.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// If this is a function or method declaration, get the accurate minArgumentCount from the @type tag, if present.
// If this is a function or method declaration, get the signature from the @type tag, if present.

// Exclude contextually-typed kinds because we already apply the @type tag to the context, plus applying it here to the initializer would supress checks that the two are compatible.
result.push(
(!isFunctionExpressionOrArrowFunction(decl) &&
!isObjectLiteralMethod(decl) &&
getSignatureOfTypeTag(decl)) ||
getSignatureFromDeclaration(decl)
);
}
return result;
}
Expand Down Expand Up @@ -12976,7 +12981,7 @@ namespace ts {
else {
const type = signature.declaration && getEffectiveReturnTypeNode(signature.declaration);
let jsdocPredicate: TypePredicate | undefined;
if (!type && isInJSFile(signature.declaration)) {
if (!type) {
const jsdocSignature = getSignatureOfTypeTag(signature.declaration!);
if (jsdocSignature && signature !== jsdocSignature) {
jsdocPredicate = getTypePredicateOfSignature(jsdocSignature);
Expand Down Expand Up @@ -17206,8 +17211,7 @@ namespace ts {
}

function isContextSensitiveFunctionLikeDeclaration(node: FunctionLikeDeclaration): boolean {
return (!isFunctionDeclaration(node) || isInJSFile(node) && !!getTypeForDeclarationFromJSDocComment(node)) &&
(hasContextSensitiveParameters(node) || hasContextSensitiveReturnExpression(node));
return hasContextSensitiveParameters(node) || hasContextSensitiveReturnExpression(node);
}

function hasContextSensitiveReturnExpression(node: FunctionLikeDeclaration) {
Expand All @@ -17216,7 +17220,7 @@ namespace ts {
}

function isContextSensitiveFunctionOrObjectLiteralMethod(func: Node): func is FunctionExpression | ArrowFunction | MethodDeclaration {
return (isInJSFile(func) && isFunctionDeclaration(func) || isFunctionExpressionOrArrowFunction(func) || isObjectLiteralMethod(func)) &&
return (isFunctionExpressionOrArrowFunction(func) || isObjectLiteralMethod(func)) &&
isContextSensitiveFunctionLikeDeclaration(func);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/checkJsdocTypeTag5.types
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var k = function (x) { return x }
/** @typedef {(x: 'hi' | 'bye') => 0 | 1 | 2} Argle */
/** @type {Argle} */
function blargle(s) {
>blargle : (s: "hi" | "bye") => 0 | 1 | 2
>blargle : (x: 'hi' | 'bye') => 0 | 1 | 2
>s : "hi" | "bye"

return 0;
Expand All @@ -55,7 +55,7 @@ function blargle(s) {
var zeroonetwo = blargle('hi')
>zeroonetwo : 0 | 1 | 2
>blargle('hi') : 0 | 1 | 2
>blargle : (s: "hi" | "bye") => 0 | 1 | 2
>blargle : (x: "hi" | "bye") => 0 | 1 | 2
>'hi' : "hi"

/** @typedef {{(s: string): 0 | 1; (b: boolean): 2 | 3 }} Gioconda */
Expand Down
31 changes: 30 additions & 1 deletion tests/baselines/reference/checkJsdocTypeTag6.errors.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
tests/cases/conformance/jsdoc/test.js(1,12): error TS8030: The type of a function declaration must match the function's signature.
tests/cases/conformance/jsdoc/test.js(7,5): error TS2322: Type '(prop: any) => void' is not assignable to type '{ prop: string; }'.
tests/cases/conformance/jsdoc/test.js(10,12): error TS8030: The type of a function declaration must match the function's signature.
tests/cases/conformance/jsdoc/test.js(23,12): error TS8030: The type of a function declaration must match the function's signature.
tests/cases/conformance/jsdoc/test.js(27,7): error TS2322: Type '(more: any) => void' is not assignable to type '() => void'.
tests/cases/conformance/jsdoc/test.js(30,7): error TS2322: Type '(more: any) => void' is not assignable to type '() => void'.
tests/cases/conformance/jsdoc/test.js(34,3): error TS2322: Type '(more: any) => void' is not assignable to type '() => void'.


==== tests/cases/conformance/jsdoc/test.js (3 errors) ====
==== tests/cases/conformance/jsdoc/test.js (7 errors) ====
/** @type {number} */
~~~~~~
!!! error TS8030: The type of a function declaration must match the function's signature.
Expand All @@ -28,4 +32,29 @@ tests/cases/conformance/jsdoc/test.js(10,12): error TS8030: The type of a functi
// TODO: Should be an error since signature doesn't match.
/** @type {(a: number, b: number, c: number) => number} */
function add3(a, b) { return a + b; }

// Confirm initializers are compatible.
// They can't have more parameters than the type/context.

/** @type {() => void} */
~~~~~~~~~~
!!! error TS8030: The type of a function declaration must match the function's signature.
function funcWithMoreParameters(more) {} // error

/** @type {() => void} */
const variableWithMoreParameters = function (more) {}; // error
~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2322: Type '(more: any) => void' is not assignable to type '() => void'.

/** @type {() => void} */
const arrowWithMoreParameters = (more) => {}; // error
~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2322: Type '(more: any) => void' is not assignable to type '() => void'.

({
/** @type {() => void} */
methodWithMoreParameters(more) {}, // error
~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2322: Type '(more: any) => void' is not assignable to type '() => void'.
});

26 changes: 26 additions & 0 deletions tests/baselines/reference/checkJsdocTypeTag6.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,29 @@ function add3(a, b) { return a + b; }
>a : Symbol(a, Decl(test.js, 17, 14))
>b : Symbol(b, Decl(test.js, 17, 16))

// Confirm initializers are compatible.
// They can't have more parameters than the type/context.

/** @type {() => void} */
function funcWithMoreParameters(more) {} // error
>funcWithMoreParameters : Symbol(funcWithMoreParameters, Decl(test.js, 17, 37))
>more : Symbol(more, Decl(test.js, 23, 32))

/** @type {() => void} */
const variableWithMoreParameters = function (more) {}; // error
>variableWithMoreParameters : Symbol(variableWithMoreParameters, Decl(test.js, 26, 5))
>more : Symbol(more, Decl(test.js, 26, 45))

/** @type {() => void} */
const arrowWithMoreParameters = (more) => {}; // error
>arrowWithMoreParameters : Symbol(arrowWithMoreParameters, Decl(test.js, 29, 5))
>more : Symbol(more, Decl(test.js, 29, 33))

({
/** @type {() => void} */
methodWithMoreParameters(more) {}, // error
>methodWithMoreParameters : Symbol(methodWithMoreParameters, Decl(test.js, 31, 2))
>more : Symbol(more, Decl(test.js, 33, 27))

});

35 changes: 33 additions & 2 deletions tests/baselines/reference/checkJsdocTypeTag6.types
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var g = function (prop) {

/** @type {(a: number) => number} */
function add1(a, b) { return a + b; }
>add1 : (a: number, b: any) => number
>add1 : (a: number) => number
>a : number
>b : any
>a + b : any
Expand All @@ -35,10 +35,41 @@ function add2(a, b) { return a + b; }
// TODO: Should be an error since signature doesn't match.
/** @type {(a: number, b: number, c: number) => number} */
function add3(a, b) { return a + b; }
>add3 : (a: number, b: number) => number
>add3 : (a: number, b: number, c: number) => number
>a : number
>b : number
>a + b : number
>a : number
>b : number

// Confirm initializers are compatible.
// They can't have more parameters than the type/context.

/** @type {() => void} */
function funcWithMoreParameters(more) {} // error
>funcWithMoreParameters : () => void
>more : any

/** @type {() => void} */
const variableWithMoreParameters = function (more) {}; // error
>variableWithMoreParameters : () => void
>function (more) {} : (more: any) => void
>more : any

/** @type {() => void} */
const arrowWithMoreParameters = (more) => {}; // error
>arrowWithMoreParameters : () => void
>(more) => {} : (more: any) => void
>more : any

({
>({ /** @type {() => void} */ methodWithMoreParameters(more) {}, // error}) : { methodWithMoreParameters(): void; }
>{ /** @type {() => void} */ methodWithMoreParameters(more) {}, // error} : { methodWithMoreParameters(): void; }

/** @type {() => void} */
methodWithMoreParameters(more) {}, // error
>methodWithMoreParameters : (more: any) => void
>more : any

});

4 changes: 4 additions & 0 deletions tests/baselines/reference/checkJsdocTypeTag7.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@ class C {
>foo : Symbol(C.foo, Decl(test.js, 4, 9))
>a : Symbol(a, Decl(test.js, 6, 8))
>b : Symbol(b, Decl(test.js, 6, 10))

/** @type {(optional?) => void} */
methodWithOptionalParameters() {}
>methodWithOptionalParameters : Symbol(C.methodWithOptionalParameters, Decl(test.js, 6, 16))
}

4 changes: 4 additions & 0 deletions tests/baselines/reference/checkJsdocTypeTag7.types
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@ class C {
>foo : (a: string, b: number) => void
>a : string
>b : number

/** @type {(optional?) => void} */
methodWithOptionalParameters() {}
>methodWithOptionalParameters : (optional?: any) => void
}

81 changes: 72 additions & 9 deletions tests/baselines/reference/jsDocDontBreakWithNamespaces.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -181,19 +181,82 @@
"kind": "space"
}
],
"parameters": [],
"documentation": [],
"tags": [
"parameters": [
{
"name": "type",
"text": [
"name": "arg0",
"documentation": [],
"displayParts": [
{
"text": "{function(module:xxxx, module:xxxx): module:xxxxx}",
"kind": "text"
"text": "arg0",
"kind": "parameterName"
},
{
"text": ":",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "any",
"kind": "keyword"
}
]
],
"isOptional": false,
"isRest": false
},
{
"name": "arg1",
"documentation": [],
"displayParts": [
{
"text": "arg1",
"kind": "parameterName"
},
{
"text": ":",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "any",
"kind": "keyword"
}
],
"isOptional": false,
"isRest": false
},
{
"name": "arg2",
"documentation": [],
"displayParts": [
{
"text": "arg2",
"kind": "parameterName"
},
{
"text": ":",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "any",
"kind": "keyword"
}
],
"isOptional": false,
"isRest": false
}
]
],
"documentation": [],
"tags": []
}
],
"applicableSpan": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ tests/cases/conformance/jsdoc/a.js(13,1): error TS2554: Expected 1 arguments, bu
g() // should error
~~~
!!! error TS2554: Expected 1 arguments, but got 0.
!!! related TS6210 tests/cases/conformance/jsdoc/a.js:5:12: An argument for 's' was not provided.
!!! related TS6210 tests/cases/conformance/jsdoc/a.js:4:13: An argument for 's' was not provided.
Copy link
Member

Choose a reason for hiding this comment

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

This error span change shows that re-using the signature isn't precisely correct. It's probably close enough, though, and it would take much more code to copy over the properties of the declared signature to the new signature.

h()
~~~
!!! error TS2554: Expected 1 arguments, but got 0.
!!! related TS6210 tests/cases/conformance/jsdoc/a.js:8:12: An argument for 's' was not provided.
!!! related TS6210 tests/cases/conformance/jsdoc/a.js:7:14: An argument for 's' was not provided.

4 changes: 2 additions & 2 deletions tests/baselines/reference/typeTagWithGenericSignature.types
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
=== tests/cases/conformance/jsdoc/bug25618.js ===
/** @type {<T>(param?: T) => T | undefined} */
function typed(param) {
>typed : <T>(param: T | undefined) => T | undefined
>typed : <T>(param?: T | undefined) => T | undefined
>param : T | undefined

return param;
Expand All @@ -11,7 +11,7 @@ function typed(param) {
var n = typed(1);
>n : number | undefined
>typed(1) : 1 | undefined
>typed : <T>(param: T | undefined) => T | undefined
>typed : <T>(param?: T | undefined) => T | undefined
>1 : 1


17 changes: 17 additions & 0 deletions tests/cases/conformance/jsdoc/checkJsdocTypeTag6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,20 @@ function add2(a, b) { return a + b; }
// TODO: Should be an error since signature doesn't match.
/** @type {(a: number, b: number, c: number) => number} */
function add3(a, b) { return a + b; }

// Confirm initializers are compatible.
// They can't have more parameters than the type/context.

/** @type {() => void} */
function funcWithMoreParameters(more) {} // error

/** @type {() => void} */
const variableWithMoreParameters = function (more) {}; // error

/** @type {() => void} */
const arrowWithMoreParameters = (more) => {}; // error

({
/** @type {() => void} */
methodWithMoreParameters(more) {}, // error
});
3 changes: 3 additions & 0 deletions tests/cases/conformance/jsdoc/checkJsdocTypeTag7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@
class C {
/** @type {Foo} */
foo(a, b) {}

/** @type {(optional?) => void} */
methodWithOptionalParameters() {}
}