question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JSDoc ...rest parameter syntax cannot support tuple types

See original GitHub issue

Bug Report

🔎 Search Terms

jsdoc rest tuple

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about JSDoc

⏯ Playground Link

Playground link with relevant code

💻 Code

/**
 * @callback CBFunction
 * @param {number} n
 * @param {string} s
 */

// Desired outcome:
// typedef CBCaller = (...args: Parameters<CBFunction>) => void

/**
 * @param {...Parameters<CBFunction>} args
 */
function f(...args) {
  console.log(args);
}

🙁 Actual behavior

See Playground link: on .d.ts tab, the output is declare function f(...args: [number, string][]): void;, an array-of-tuples. Generally, JSDoc syntax treats {...X} args as args: X[].

🙂 Expected behavior

TS-flavored JSDoc should be able to express any type that TS can express.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
sandersncommented, Jul 14, 2022

TS’ jsdoc doesn’t currently have a good way to match any rest parameter to a @param tag. The code you wrote is unfortunately already used by closure type syntax in jsdoc, so this

/**
 * @param {...Parameters<CBFunction>} args
 */
function f(...args)

Is like writing this:

function f(...args: Parameters<CBFunction>[])

even if it weren’t used, everything inside {} is supposed to be a type.

We need an alternate proposal. One possibility is to add the … on the parameter name:

/**
 * @param {Parameters<CBFunction>} ...args
 */
function f(...args)

This might conflict with existing jsdoc, but I don’t think it does off the top of my head. And I think it would be possible to enforce the usual rules on ...: eg that it can only occur once, at the end of a parameter list.

0reactions
thw0rtedcommented, Jul 25, 2022

tsd-jsdoc does not currently use the TS compiler, though they have been thinking about it for a while. I believe the answer to your question is, they only support a subset of all the JSDoc constructs that the TS compiler does.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JSDoc Reference - TypeScript: Documentation
JSDoc Reference. The list below outlines which constructs are currently supported when using JSDoc annotations to provide type information in JavaScript files.
Read more >
Joshua's Docs - JSDoc Cheatsheet and Type Safety Tricks
VSCode now supports Tuple types expressed through JSDoc, in a variety of different ways. Standard Tuple Type: /** @typedef {[string, number]} ...
Read more >
Typescript: What is the point of using a tuple as a rest ...
The rest tuple syntax lets you abstract over function parameter lists in a way that a regular call signature does not.
Read more >
Overview - TypeScript
We can prefix any tuple type with the readonly keyword to make it a readonly ... The declarations use generic rest parameters (see...
Read more >
August 2020 - TypeScript - Microsoft Developer Blogs
To deepen the connection between parameter lists and tuple types, the syntax for rest elements and optional elements mirrors the syntax for ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found