Javascript 如何使用内联 JSDoc 指示参数是可选的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5873927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:18:50  来源:igfitidea点击:

How to indicate param is optional using inline JSDoc?

javascriptgoogle-closure-compilerjsdoc

提问by studgeek

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

根据@param的 JSDoc wiki,您可以指示 @param 是可选的,使用

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

and you can indicate a param inlineusing

并且您可以使用内联指示参数

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

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

我可以像下面这样组合它们,这可以正常工作。

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

But I would like to know if there is a way to do it all inline if possible.

但我想知道是否有办法在可能的情况下内联完成所有操作。

采纳答案by studgeek

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) {}

我找到了一种使用 Google Closure Compiler类型表达式来做到这一点的方法。你在类型后面放一个等号,如下所示: function test(/**String=*/arg) {}

回答by czerny

From official documentation:

来自官方文档

Optional parameter

可选参数

An optional parameter named foo.

一个名为 foo 的可选参数。

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

An optional parameter foo with default value 1.

可选参数 foo,默认值为 1。

@param {number} [foo=1]

回答by vvMINOvv

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) {}

只是比视觉上更具吸引力 function test(/**String=*/arg) {}

回答by Tomá? Hübelbauer

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 undefinedyou 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):

如果您对函数参数使用内联类型注释,并且想知道如何在该表示法中将函数参数标记为可选,我发现只需为可选参数分配默认值即可。如果你想要默认值,undefined你也必须明确设置它,否则参数不会被标记为可选(即使它前面已经是可选参数):

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

If you hover over demoin your IDE you should see both optional1and optional2showing up as optional now. In VSCode that is indicated by ?after the argument name (TypeScript notation). If you remove = undefinedfrom optional2you will see only optional1being optional which is of course nonsense so the default value here must be explicit like I alluded to in the above paragraph.

如果您将鼠标悬停demo在您的 IDE 中,您现在应该看到两者optional1optional2显示为可选。在 VSCode 中,由?参数名称(TypeScript 表示法)后表示。如果你= undefined从中删除,optional2你只会看到optional1可选的,这当然是无稽之谈,所以这里的默认值必须是明确的,就像我在上一段中提到的那样。