开发者

How to indicate param is optional using inline JSDoc?

开发者 https://www.devze.com 2023-03-02 19:24 出处:网络
According to the JSDoc wiki for @param you can indicate a @param is optional using /** @param {String} [name]

According to the JSDoc wiki for @param you can indicate a @param is optional using

/**
    @param {String} [name]
*/
function getPerson(name) {
}

and you can indicate a param inline using

function getPerson(/**String*/ name) {
}

And I can combine them like the following, which works ok.

/**
    @param [name]
*/
function getPerson(/**String*/name) {
}

But I woul开发者_JS百科d like to know if there is a way to do it all inline if possible.


From official documentation:

Optional parameter

An optional parameter named foo.

@param {number} [foo]
// or:
@param {number=} foo

An optional parameter foo with default value 1.

@param {number} [foo=1]


After some digging up I found these are ok as well

/**
 * @param {MyClass|undefined}
 * @param {MyClass=}
 * @param {String} [accessLevel="author"] The user accessLevel is optional.
 * @param {String} [accessLevel] The user accessLevel is optional.
 */

Just slightly more visually appealing than function test(/**String=*/arg) {}


I found a way to do this using Google Closure Compiler type expressions. You put an equals sign after the type like so: function test(/**String=*/arg) {}


In case you are using inline type comments on function arguments and are wondering how to mark a function argument as optional in that notation, I found that just assigning default values to the optional arguments worked. If you want the default to be undefined you have to set it explicitly as well though, otherwise the argument won't be marked as optional (even if it preceded by already optional arguments):

function demo(
  /** @type {String} */ mandatory,
  /** @type {Number} */ optional1 = 0,
  /** @type {Number} */ optional2 = undefined
)

If you hover over demo in your IDE you should see both optional1 and optional2 showing up as optional now. In VSCode that is indicated by ? after the argument name (TypeScript notation). If you remove = undefined from optional2 you will see only optional1 being optional which is of course nonsense so the default value here must be explicit like I alluded to in the above paragraph.


The most complete answer will be from the official typescript documentation.

// Parameters may be declared in a variety of syntactic forms
/**
 * @param {string}  p1 - A string param.
 * @param {string=} p2 - An optional param (Google Closure syntax)
 * @param {string} [p3] - Another optional param (JSDoc syntax).
 * @param {string} [p4="test"] - An optional param with a default value
 * @returns {string} This is the result
 */


With TypeScript's handling of JSDoc, this is not possible: https://github.com/microsoft/TypeScript/issues/47653

While you can mark a param as @type { ... | undefined }, its optionality does not change, because all parameters are optional.

The way you mark optionality/non-optionality is by whether you put the param name in brackets, but there's no param name in this syntax, so only modifying the type (rather than also modifying the optionality in a way you can't control) is the most intuitive thing to do.

So with TS, you'll have to go with external @param comment blocks.

0

精彩评论

暂无评论...
验证码 换一张
取 消