JSDoc: Define allowed values of parameter which has string type
Asked Answered
B

2

8

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?

Babbling answered 22/12, 2017 at 21:29 Comment(0)
C
20

Use a type union |:

/**
 * Description
 * @param {('open'|'close')}  operator
 */
Calycine answered 22/12, 2017 at 21:40 Comment(0)
S
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://mcmap.net/q/150276/-how-to-document-a-string-type-in-jsdoc-with-limited-possible-values

Alternatively, you can create wrapper functions like

_changeState(state){...} // assumed private
close(){ _changeState('close')}
open(){_changeState('open')}
Steelmaker answered 22/12, 2017 at 21:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.