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 support for array destructuring

See original GitHub issue

Currently there is no simple way to annotate destructured array parameters.

I propose a syntax similar to the current destructured objects syntax:

/**
 * @param {[x: number, y: number]} randomName
 * @return number
 */
function add([x, y]) {
    return x + y;
}

Due to the nature of destructured arrays, this issue is closely related to #379.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:18
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
heydarmcommented, Apr 26, 2021

These work in VS Code:

/**
 * @param {[x: number, y: number]} param
 * @returns {[x: number, y: number]}
 */
function doNothing([x, y]) {
    return [x, y];
}

and

/**
 * @param {[number, number]} param
 * @returns {[number, number]}
 */
function doNothing([x, y]) {
    return [x, y];
}
2reactions
robfigcommented, Oct 10, 2020

Thanks for the guidance.

Since calling React.useState is a common thing to do, I was hoping to make it possible to do so with less ceremony. Going through a wrapper seems like it might be able to streamline things while maintaining type checks:

/**
* @template T
* @param {!T} value
* @return {{val: !T, set: function(!T)}}
*/
WrapReact.useState = function(value) {
  const state = React.useState(value)
  const val = /** !T */ (state[0]);
  const set = /** function(!T) */ (state[1]);
  return {val: val, set: set}
}

const num = WrapReact.useState(Math.random());

console.log(num.val);
num.set(Math.random());

It seems to work in the online playground

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to document array destructured parameter in JSDoc
Given the following code, how do I properly document that using the latest JSDoc? function ...
Read more >
JSDoc @type doesn't work with array destructuring (but does ...
JSDoc @type doesn't work with array destructuring (but does with object destructuring) In the above, foo doesn't get typed to string , but...
Read more >
Use JSDoc: @param
If a parameter is destructured without an explicit name, you can give the object an appropriate one and document its properties. Documenting a...
Read more >
Destructuring assignment - JavaScript - MDN Web Docs
The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from ...
Read more >
Airbnb JavaScript Style Guide()
Types; References; Objects; Arrays; Destructuring; Strings; Functions ... not be used when targeting browsers/environments that don't support them natively.
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