3

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?

4 Answers 4

16

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

Sign up to request clarification or add additional context in comments.

Comments

2

Optional parameters can be put in [] square brackets

/**
 * @param {any} param1
 * @param {Object} [param2]
 * 
 * @returns {Object}
 */
function doSomething (param1, { property1 = null, property2 = null }) {}

However, I don't think param2 can be optional AND destructured like that. That won't work.

Comments

1

You can refer to the official js doc website pertaining to default values, optional parameter etc. Reference: https://jsdoc.app/tags-param.html#optional-parameters-and-default-values

Look for Parameters with properties.

Comments

0

If you're writing JSDocs for the Closure Compiler you should use = to denote optional parameters:

* @param {string=} property1

https://github.com/google/closure-compiler/wiki/Types-in-the-Closure-Type-System#optional

But for Object properties, you can use a record type with |undefined:

/** @param {{required:string, optional:(string|undefined)}} props */

https://github.com/google/closure-compiler/wiki/A-word-about-the-type-Object#records

Here's a version in the Closure Compiler API thingy

Note: I do think this is probably a relatively new feature in the compiler, so your mileage may vary.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.