Oliver Watkins
Oliver Watkins

Reputation: 13489

How do you document an array of objects as a parameter in JSDoc?

I have an array that looks like this:

[{
    "name": "c917379",
    "email": "[email protected]"

},
{
    "name": "c917389",
    "email": "[email protected]"
}]

It is an array of arbitrary length with a number of repeating fields (I've reduced this to two fields for clarity). This gets passed into a JavaScript method.

/**
 * @param {?}  data
 */
update:  function(data) {...}

I was wondering how you would document this in JSDoc. Ie. how would you document the type where the question mark is?

Upvotes: 15

Views: 9338

Answers (4)

Wilt
Wilt

Reputation: 44326

In JSDoc there is an example given for an array with members of type MyClass. It looks like this:

@param {Array.<MyClass>}

So then you can also do like this:

@param {Array.<Object>}

And then this also makes sense:

@param {Array.<{name:string, email:string}>}

Upvotes: 29

Willian Soares
Willian Soares

Reputation: 320

Since this is the first question that appears on Google, i think that is useful show how to documment an two dimensional array.

The default sintax doesn't work, it shows like 'JsDoc sinxtax error':

/**
 * @param {Object[][]} a two dimensional array of object
 * */

The correct way to signal a two dimensional array is:

/**
 * @param {Array.<Array.<Object>>} a two dimensional array of object
 * */

Upvotes: 2

Oliver Watkins
Oliver Watkins

Reputation: 13489

I just figured out the answer to my question :

It would look like this :

/**
 *
 * @param {{name:string, email:string}[]}  
 *
 */

Upvotes: 25

Alnitak
Alnitak

Reputation: 339786

Since there's nothing intrinsically "special" with your object contents I believe you would just have to declare it as:

@param {Object[]} data

The alternative would be to declare a "proper" constructor function for your "class", and then replace Object with that function name.

This encapsulation might also help other parts of your code ;-)

Upvotes: 10

Related Questions