186

In JSDoc, the best documentation I can find shows to use the following if you have an array of a specific type (such as an array of strings) as:

/**
 * @param {Array.<string>} myStrings All my awesome strings
 */
 function blah(myStrings){
     //stuff here...
 }

How would you replace the below question marks specify an array of objects?

/**
 * @param {???????} myObjects All of my equally awesome objects
 */
 function blah(myObjects){
     //stuff here...
 }
Ray
  • 40,256
  • 21
  • 101
  • 138
  • 5
    possible duplicate of [Document collection (array of type) return value and parameter in JSDoc](http://stackoverflow.com/questions/8498975/document-collection-array-of-type-return-value-and-parameter-in-jsdoc) – Gajus Aug 21 '15 at 12:08

1 Answers1

311

You should be more specific what you mean by JSDoc - this is a generic term covering pretty much all the JavaDoc-style documentation tools for JavaScript.

The syntax you used for array of strings looks like the one supported by Google Closure Compiler.

Using this, an array of Objects would be:

/**
 * @param {Array.<Object>} myObjects
 */

Or just an array of anything - this should work with pretty much all doc tools:

/**
 * @param {Array} myArray
 */

jsdoc-toolkit, JSDoc 3, and JSDuck support the following syntax to denote an array of objects:

/**
 * @param {Object[]} myArray
 */

EDIT

In case you know the keys and the variable type of the values you can also do:

/**
 * @param {Array.<{myNumber: Number, myString: String, myArray: Array}>} myObjects
 */

or

/**
 * @param {{myNumber: Number, myString: String, myArray: Array}[]} myObjects
 */
Community
  • 1
  • 1
Rene Saarsoo
  • 13,580
  • 8
  • 57
  • 85
  • 27
    The . notation is now deprecated and its support should be dropped later. Current correct version is `{Array}`. Just to keep this post up to date. – Kenny806 Oct 06 '15 at 08:05
  • 3
    With JSDoc 3, how would you doc an array of String arrays? In the old syntax I might do something like `Array.` – Snekse Oct 08 '15 at 15:47
  • 10
    @Kenny806 Deprecated? A reference document please? – Wilt Dec 15 '15 at 16:44
  • 3
    @Wilt: the [JSDoc documentation is contradictory](https://github.com/jsdoc3/jsdoc/issues/1375) about the dot before angle brackets. – Dan Dascalescu Jun 12 '17 at 05:24
  • 7
    This answer doesn't explain how to declare the keys of the objects in that array, and how to declare an array of objects with specific keys as a return type. [This answer](https://stackoverflow.com/questions/8498975/document-collection-array-of-type-return-value-and-parameter-in-jsdoc/32139970#32139970) does. – Dan Dascalescu Jun 12 '17 at 05:26
  • @DanDascalescu I added such an example to my answer. I used this myself many times but it only makes sense if you know these details otherwise the more general object approach is better... – Wilt Jun 12 '17 at 07:14
  • @Snekse, I know this is old, but you'd do {String[][]} – RoboticRenaissance Jun 14 '19 at 13:24
  • @RoboticRenaissance I haven't had to use it in a long time, so I wouldn't know any better to not use it. – Snekse Jun 15 '19 at 20:48
  • Agree with Kenny806, but `Array.<>` is valid for non-Closure Compiler users. – Graham P Heath Jun 20 '19 at 22:10