Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to describe an optional object parameter with default values using JSDocs

I don't if the title properly expressed what I am trying to do. But I have the following function:

/**
 * @param {any} param1
 * How to describe the second parameter??
 * @returns {Object}
 */
function doSomething (param1, { property1 = null, property2 = null }){
  // do stuff
  return something
}

As questioned in the comment, using JSDocs, how would I describe the second parameter?

like image 706
theJuls Avatar asked May 18 '26 07:05

theJuls


1 Answers

Use square brackets [] to indicate optional parameters. Like so:

/**
 * @param {any} param1
 * @param {Object} somethingWithProps - Some description
 * @param {string} [somethingWithProps.property1] - First property
 * @param {string} [somethingWithProps.property2] - Second property
 * @returns {Object}
 */
function doSomething (param1, { property1 = null, property2 = null }){
    // do stuff
    return something
}

From the docs: Optional parameters and Documenting a destructuring parameter

like image 51
n8jadams Avatar answered May 19 '26 20:05

n8jadams