javascript - Google Closure Compiler 用于变量参数的 @param 注释

标签 javascript annotations google-closure-compiler jsdoc

我有一个可以接受可变数量参数的函数。

根据Google Closure Compiler wiki ,这是使用 @param 注解的方法。

/**
 * Takes 2 or more strings and do something cool with them.
 * @param {...string} var_args
 * @return {string} the processed result
 */
function doSomethingCool() {
    var len = arguments.length;
    if (len < 2) {
        throw Error('Need at least 2 arguments');
    }

    ...
}

问题

当我尝试编译它时,我看到此警告:JSC_INEXISTENT_PARAM:参数 var_args 未出现在 doSomethingCool 的参数列表中第 6 行字符 1

所以我尝试了@param {string}参数,但同样的错误。

我还尝试了没有变量名的@param {string}。我得到:JSC_TYPE_PARSE_ERROR:错误的类型注释。需要 @param 标记中的变量名称。

问题

我做错了什么以及如何为闭包编译器注释可变参数?

最佳答案

您的 var_args 需要实际出现在参数列表中,这就是错误告诉您的内容。

/**
 * Takes 2 or more strings and do something cool with them.
 * @param {...string} var_args
 * @return {string} the processed result
 */
function doSomethingCool(var_args) {}

闭包编译器将识别出它从未被引用,并在编译期间将其删除。

如果列出它让您感到困扰,您可以使用 @type 注释来代替:

/**
 * Takes 2 or more strings and do something cool with them.
 * @type {function(...string):string}
 */
function doSomethingCool() {}

如果您确实想要正确的类型检查,请注释该函数,以便它需要 2 个或更多字符串:

/**
 * Takes 2 or more strings and do something cool with them.
 * @type {function(string, string, ...string):string}
 */
function doSomethingCool() {}

关于javascript - Google Closure Compiler 用于变量参数的 @param 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45474397/

相关文章:

javascript - 无法解析模块说明符 "three"。相对引用必须以 "/"、 "./"或 "../"开头

node.js - 获取导入模块的文件路径

javascript - 将 NodeList 转换为 Array.<HTMLElement> 在 javascript 中

google-closure-compiler - 如何包含谷歌关闭的依赖项

javascript - Closure 编译器不支持 ES6

javascript - 不使用 angularjs 1.6 从 JSON 文件获取数据

javascript - javascript方法中的美元符号

java - Jackson:JsonIninclude 如何添加多个 JsonIninclude 注释

没有 bean 定义的 Spring @configure 注解用法

javascript - 无法在功能后停止快速服务器