BuZZ-dEE
BuZZ-dEE

Reputation: 6929

JSDoc required parameter with default value

I found the following for document an optional parameter in JSDoc.:

/**
 * @param {string} [somebody=John Doe] - Somebody's name.
 */
function sayHello(somebody) {
    if (!somebody) {
        somebody = 'John Doe';
    }
    alert('Hello ' + somebody);
}

and this for document parameters object properties.

/**
 * Assign the project to an employee.
 * @param {Object} employee - The employee who is responsible for the project.
 * @param {string} employee.name - The name of the employee.
 * @param {string} employee.department - The employee's department.
 */
Project.prototype.assign = function(employee) {
    // ...
};

Now I want document a required object parameter in JSDoc. Something like this.:

/**
 * Assign the project to an employee.
 * @param {Object} employee - The employee who is responsible for the project.
 * @param {string} employee.name=John Doe - The name of the employee.
 * @param {string} employee.department - The employee's department.
 */
Project.prototype.assign = function(employee) {
    // ...
};

How can I do that?

Maybe the solution @param {string} employee.name=John Doe - The name of the employee. is already the correct one?

Upvotes: 3

Views: 10097

Answers (1)

machineghost
machineghost

Reputation: 35790

You are correct. Per "Parameters with properties" in https://jsdoc.app/tags-param.html:

If a parameter is expected to have a specific property, you can document that property by providing an additional @param tag.

However, if you're going to re-use those properties in other functions you might want to instead define an Employee type:

/**
 * The employee who is responsible for the project
 * @typedef {Object} Employee
 * @property {string} name - The name of the employee
 * etc.
 */

/*
 * @param {Employee} employee - The employee who is responsible for the project.
 */
Project.prototype.assign = function(employee) {

https://jsdoc.app/tags-typedef.html

Upvotes: 3

Related Questions