9

I'm using JSDoc and I would like to add the info to my documentation which value a parameter should have.

In this example you can see, that the parameter operator has string type. But furthermore there can only be open or close as valid value of the parameter

/**
 * Description
 * @param {string='open','close'}  operator
 */

What is the correct syntax to add this information?

2 Answers 2

21

Use a type union |:

/**
 * Description
 * @param {('open'|'close')}  operator
 */
Sign up to request clarification or add additional context in comments.

Comments

0

From a design perspective, if your pram is restricted to a list of options you should create an Enum of those options for reusability. see this answer for details: https://stackoverflow.com/a/19322623/5633515

Alternatively, you can create wrapper functions like

_changeState(state){...} // assumed private
close(){ _changeState('close')}
open(){_changeState('open')}

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.