24

I have a function with an array of objects as parameter and would like to describe the parameter (including the properties of the objects in the array) using JSDOC like in this example:

/**
 * @param {Array.<Object>} filter - array of filter objects
 * @param ...
 */
function doSomething(filter) {
}

where filter is something like this:

filter = [
   {id: 'session', value: 1},
   {id: 'name', value: 'john'}
]

How would I document the properties id and value in jsdoc3 ?

1

1 Answer 1

40

like this:

/**
 * @param {Object[]} filter - a list of literal filter objects
 * @param {string} filter[].id -  id to filter against...
 * @param {string|number} filter[].value - value to filter for...
 */
function doSomething(filter) {
    // do stuff
}

taken from http://usejsdoc.org/tags-param.html

2
  • Is there any way to use this with @typedef? I want to essentially do the exact same thing, except in an @typedef {Object[]} filter instead of @param {Object[]} filter, since I reuse the type a lot. I've tried the same thing but changing the @params (excluding the first, which becomes @typedef) to @type, @typedef and @param, but nothing seems to be working. Jan 22, 2020 at 17:11
  • 2
    I prefer using Array.<Object> over Object[], e.g. @param {Array.<Object>} filter. However, VSCode only recognized the array item's properties when using the Object[] syntax for the array. Is there a different method of specifiying array item properties when using Array.<Object>? Mar 18, 2020 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.