18

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 ?

doberkofler
  • 6,047
  • 10
  • 54
  • 91
  • 3
    Possible duplicate of [Document collection (array of type) return value and parameter in JSDoc](https://stackoverflow.com/questions/8498975/document-collection-array-of-type-return-value-and-parameter-in-jsdoc) – Dan Dascalescu Jun 12 '17 at 05:14

1 Answers1

29

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

stolli
  • 4,405
  • 2
  • 22
  • 36
  • 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 `@param`s (excluding the first, which becomes `@typedef`) to `@type`, `@typedef` and `@param`, but nothing seems to be working. – Geza Kerecsenyi Jan 22 '20 at 17:11
  • 1
    I prefer using `Array.` over `Object[]`, e.g. `@param {Array.} 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.`? – Felix Edelmann Mar 18 '20 at 16:37