How to describe destructured object arguments in JSDoc
Asked Answered
S

2

15

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;
Smithsonite answered 20/6, 2017 at 5:48 Comment(3)
We still doesn't have an good answerConcessionaire
@Maxwells.c what's wrong with the accepted answer?Smithsonite
nevermind. It's an good solution and works. I was just looking for an less verbose one. For instance, JSDoc accepts this @param {Object} { bar, qux }. Would be great to reffer bar an gux describing then this way, less code.Concessionaire
R
17

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;
Rataplan answered 20/6, 2017 at 6:34 Comment(3)
This does not work for me in VSCode 1.23.1. Is it a VSCode limitation?Grantee
Notice that the official JSDoc documentation for this feature is now here.Grantee
Here is a specific question about this feature not working in VSCode.Grantee
A
1

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}) => {

}
Annulet answered 12/5, 2018 at 13:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.