Skip to content

Commit 2bb54dc

Browse files
authored
Fix getTypeFromJSDocVariadicType in callback tag (#43562)
* Fix getTypeFromJSDocVariadicType in @callback Variadics have never worked there, I think. * add test + fix lint * remove outdated comment
1 parent c1923e9 commit 2bb54dc

File tree

5 files changed

+90
-2
lines changed

5 files changed

+90
-2
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38433,15 +38433,18 @@ namespace ts {
3843338433
if (isJSDocTypeExpression(node.parent) && isJSDocParameterTag(paramTag)) {
3843438434
// Else we will add a diagnostic, see `checkJSDocVariadicType`.
3843538435
const host = getHostSignatureFromJSDoc(paramTag);
38436-
if (host) {
38436+
const isCallbackTag = isJSDocCallbackTag(paramTag.parent.parent);
38437+
if (host || isCallbackTag) {
3843738438
/*
3843838439
Only return an array type if the corresponding parameter is marked as a rest parameter, or if there are no parameters.
3843938440
So in the following situation we will not create an array type:
3844038441
/** @param {...number} a * /
3844138442
function f(a) {}
3844238443
Because `a` will just be of type `number | undefined`. A synthetic `...args` will also be added, which *will* get an array type.
3844338444
*/
38444-
const lastParamDeclaration = lastOrUndefined(host.parameters);
38445+
const lastParamDeclaration = isCallbackTag
38446+
? lastOrUndefined((paramTag.parent.parent as unknown as JSDocCallbackTag).typeExpression.parameters)
38447+
: lastOrUndefined(host!.parameters);
3844538448
const symbol = getParameterSymbolFromJSDoc(paramTag);
3844638449
if (!lastParamDeclaration ||
3844738450
symbol && lastParamDeclaration.symbol === symbol && isRestParameter(lastParamDeclaration)) {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//// [callbackTagVariadicType.js]
2+
/**
3+
* @callback Foo
4+
* @param {...string} args
5+
* @returns {number}
6+
*/
7+
8+
/** @type {Foo} */
9+
export const x = () => 1
10+
var res = x('a', 'b')
11+
12+
13+
//// [callbackTagVariadicType.js]
14+
"use strict";
15+
/**
16+
* @callback Foo
17+
* @param {...string} args
18+
* @returns {number}
19+
*/
20+
exports.__esModule = true;
21+
exports.x = void 0;
22+
/** @type {Foo} */
23+
var x = function () { return 1; };
24+
exports.x = x;
25+
var res = (0, exports.x)('a', 'b');
26+
27+
28+
//// [callbackTagVariadicType.d.ts]
29+
/**
30+
* @callback Foo
31+
* @param {...string} args
32+
* @returns {number}
33+
*/
34+
/** @type {Foo} */
35+
export const x: Foo;
36+
export type Foo = (...args: string[]) => number;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/conformance/jsdoc/callbackTagVariadicType.js ===
2+
/**
3+
* @callback Foo
4+
* @param {...string} args
5+
* @returns {number}
6+
*/
7+
8+
/** @type {Foo} */
9+
export const x = () => 1
10+
>x : Symbol(x, Decl(callbackTagVariadicType.js, 7, 12))
11+
12+
var res = x('a', 'b')
13+
>res : Symbol(res, Decl(callbackTagVariadicType.js, 8, 3))
14+
>x : Symbol(x, Decl(callbackTagVariadicType.js, 7, 12))
15+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
=== tests/cases/conformance/jsdoc/callbackTagVariadicType.js ===
2+
/**
3+
* @callback Foo
4+
* @param {...string} args
5+
* @returns {number}
6+
*/
7+
8+
/** @type {Foo} */
9+
export const x = () => 1
10+
>x : Foo
11+
>() => 1 : () => number
12+
>1 : 1
13+
14+
var res = x('a', 'b')
15+
>res : number
16+
>x('a', 'b') : number
17+
>x : Foo
18+
>'a' : "a"
19+
>'b' : "b"
20+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// @declaration: true
2+
// @outDir: bin/
3+
// @checkJs: true
4+
// @Filename: callbackTagVariadicType.js
5+
6+
/**
7+
* @callback Foo
8+
* @param {...string} args
9+
* @returns {number}
10+
*/
11+
12+
/** @type {Foo} */
13+
export const x = () => 1
14+
var res = x('a', 'b')

0 commit comments

Comments
 (0)