javascript - 如何用可重复的参数来表示多种类型的参数?

标签 javascript jsdoc

/**
 * Returns the sum of all numbers passed to the function.
 * @example
 * sum([1,2,3])
 * //returns 6
 * sum(1,2,3)
 * //returns 6
 * @param {number[]|...number} params - ???
 */
function sum(params) {
    var args = params instanceof Array ? params : arguments;
    for (var i = 0, sum = 0; i < args.length; i++)
        sum += args[i];
    return sum;
}

'params'可以是数组或可重复的数字

但是“@param {number[]|...number}”不工作。

书面文档输出与“@param {number[]|number}”没有区别

表明这种情况的最佳方式是什么?

最佳答案

诀窍是使用括号。我在工作中使用了以下内容。

* @param {...(object|string)} col - An optional set of columns to select.

更新了您的参数名称,这应该有效:

* @param {...(number[]|number)} params - ???

我找不到括号的任何文档,但在 JSDoc 3.4.2 下可以解决问题。

这条评论:

/**
 * Select columns manually.
 * @param {...(object|string)} col - An optional set of columns to select.
 * Each argument can either be a fully-qualified column name in the form
 * &lt;table-alias&gt;.&lt;column-name&gt;, or an object with the following
 * properties below.  If no columns are specified, then all columns are
 * selected.
 * @param {string} col.column - The fully-qualified column name.
 * @param {string} col.mapTo - The name of the property that the column
 * should be mapped to in the resulting normalized object.  @param
 * @param {function} col.convert - A converter function that takes a single value
 * from the database and transforms it.  For example, a function that
 * converts a bit from the database to a boolean value.
 * @return {this}
 */

生成此文档:

enter image description here

希望对您有所帮助!

关于javascript - 如何用可重复的参数来表示多种类型的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35692272/

相关文章:

javascript - ESLint JSDoc 回调错误

javascript - 枚举作为 JSDoc 中的 @param 类型

javascript - jQuery tablesorter 外部日期范围过滤器

javascript - 是否可以将 Tilestache 与 HERE map Javascript API 一起使用?

svn - JavaScript SVN 包装器

javascript - 如何在 JSDoc 中描述解构的对象参数

Vim JSDoc、PHPDoc、JavaDoc、RDoc 信息

javascript - Closure Compiler 并不总是强制类型安全?

javascript - 如何将 css 像素转换为 css3 比例 (x,y)

javascript - Meteor 具有 blaze(网络)和 Angular(移动)