Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Describing an array of objects in JSDoc

I've got a function which takes an array of objects.
Looks like this.

myAwesomeFunction([
    {
        name: 'someName',
        next: false,
        test: 'test'
    },
    {
        name: 'nameTwo',
        next: true
    }
]);

So far my JSDoc looks like this

/**
 * My description
 * @param {Array.<Object>}
 */

But how can I describe the object properties, types and descriptions and if they are optional of the object?

Thank you.

like image 722
Per Pettersen Avatar asked Oct 09 '16 11:10

Per Pettersen


People also ask

How to specify type in JSDoc?

The @type tag allows you to provide a type expression identifying the type of value that a symbol may contain, or the type of value returned by a function. You can also include type expressions with many other JSDoc tags, such as the @param tag.

What does @param do in JavaScript?

The @param tag provides the name, type, and description of a function parameter. The @param tag requires you to specify the name of the parameter you are documenting. You can also include the parameter's type, enclosed in curly brackets, and a description of the parameter.

What is JSDoc comment?

JSDoc comments are used for documentation lookup with Ctrl+Q in JavaScript and TypeScript, see JavaScript documentation look-up and TypeScript documentation look-up, as well as for type annotations and method return type hints in chained methods.

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.


2 Answers

JSDoc @param documentation

/**
 * Assign the project to a list of employees.
 * @param {Object[]} employees - The employees who are responsible for the project.
 * @param {string} employees[].name - The name of an employee.
 * @param {string} employees[].department - The employee's department.
 */
Project.prototype.assign = function(employees) {
    // ...
};
/**
like image 167
Danil Gudz Avatar answered Oct 06 '22 18:10

Danil Gudz


Using typedef

/**
 * @typedef AwesomeObject
 * @type {Object}
 * @property {string} name
 * @property {boolean} next
 * @property {string} test
 */

/**
 * @param {Array.<AwesomeObject>} awesomeObjects Awesome objects.
 */
myAwesomeFunction(awesomeObjects) { ... }
like image 45
Yves M. Avatar answered Oct 06 '22 17:10

Yves M.