14

If I have a JavaScript function taking an object as a parameter, I can describe expected properties of the object with JSDoc like this:

/**
 * @param bar
 * @param bar.baz {number}
 * @param bar.qux {number}
 */
function foo(bar) {
    return bar.baz + bar.qux;
}

How do I describe these properties if I define my function with ECMAScript 6 destructuring, not giving the real parameter object a name at all?

const foo = ({ baz, qux }) => baz + qux;
Henrik
  • 4,254
  • 15
  • 28

2 Answers2

16

It turns out JSDoc does support destructing via making up a placeholder name. It is lacking in official documentation.

http://usejsdoc.org/tags-param.html#parameters-with-properties

/**
 * @param {Object} param - this is object param
 * @param {number} param.baz - this is property param
 * @param {number} param.qux - this is property param
 */
const foo = ({ baz, qux }) => baz + qux;
Peter Kota
  • 8,048
  • 5
  • 25
  • 51
  • This does not work for me in VSCode 1.23.1. Is it a VSCode limitation? – steph643 Jun 01 '18 at 13:32
  • 1
    Notice that the official JSDoc documentation for this feature is now [here](http://usejsdoc.org/tags-param.html#parameters-with-properties). – steph643 Jun 01 '18 at 13:38
  • [Here](https://stackoverflow.com/questions/50655416/jsdoc-comments-for-destructuring-parameters-not-working-in-vscode) is a specific question about this feature not working in VSCode. – steph643 Jun 06 '18 at 12:31
0

I had the same question too. Now I am using Visual Code Studi, its plugin does something like this (this is suitable for me):

/**
 * @param  {} {a
 * @param  {} b
 * @param  {} c}
 * @param  {} {d}
 */
const aaa = ({a,b,c},{d}) => {

}
John Smith
  • 1,204
  • 3
  • 22
  • 42