Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Questions about documenting nested array and object data with jsDoc

How do I format nested arrays and objects using jsdoc?

This is my best guess:

an_obj = { 
        username1 : [
            {
                param1 : "value 1-1-1",
                param2 : "value 1-1-2",
                optional_nested : "1-1--2"
            },
            {
                param1 : "value 1-2-1",
                param2 : "value 1-2-2"
            },
        ],
        username2 : [
            {
                param1 : "value 2-1-1",
                param2 : "value 2-1-2"
            },
            {
                param1 : "value 2-2-1",
                param2 : "value 2-2-2",
                optional_nested : "2-2--2"              

            }
        ]
    }
}


/**
 * A function description.
 * @param {Object} obj
 * @param {Object} obj.username  This is not the object name, but a name type. 
 * @param {Array}  obj.username.array Desc...  using [] would conflict with optional params.
 *                                    However this could be confused with an object called array.
 * @param {String} obj.username.array.param1 Desc... This is the object name rather than a type.
 * @param {String} obj.username.array.param2 Desc... 
 * @param {String} obj.username.array.[optional_param] Desc... 
 */
var myFunc = function(obj){
    //...
};
myFunc(an_obj);

How do I indicate that an object is indexed by a kind of string?

How do I define a nested array?

Also not sure where to put the square brackets in the optional parameter.

like image 609
SystemicPlural Avatar asked Feb 20 '12 16:02

SystemicPlural


People also ask

Does JSDoc work with TypeScript?

You can use most JSDoc type syntax and any TypeScript syntax, from the most basic like string to the most advanced, like conditional types.

What is nested object in JavaScript?

The basic definition of an object in JavaScript is a container for named values called properties (keys). Sometimes, we need to create an object inside another object. In this case, it's called a nested object.


2 Answers

You can try doing it like this : (as specified here http://code.google.com/p/jsdoc-toolkit/wiki/TagParam)

/**
* @param {String[]} obj.username.array
*/

This is supposed to declare obj.username.array as an array of strings. Are you using jsdoc or jsdoc3?

like image 69
Armel Larcier Avatar answered Oct 19 '22 09:10

Armel Larcier


I'd recommend checking this out. I'd probably write it something like this:

//{Object{username:<Array<Object{param1:String, param2:String,[optional_nest]:string}>>}}
like image 21
Jon Wells Avatar answered Oct 19 '22 08:10

Jon Wells