2017-08-03 79 views
0

我有一個函數可以接受可變數量的參數。Google Closure Compiler @param註釋變量參數

根據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: parameter var_args does not appear in doSomethingCool's parameter list at line 6 character 1

於是,我就@param {string} arguments,但同樣的錯誤。

我也試過@param {string}沒有變量名稱。我:JSC_TYPE_PARSE_ERROR: Bad type annotation. expecting a variable name in a @param tag.

問題

我做了什麼錯誤以及如何變量參數進行註釋,以便關閉編譯器?

回答

1

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

閉合編譯器將認識到它從未被引用並在編譯過程中將其刪除。因此,它需要2個或多個字符串

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

如果你真的想正確的類型檢查,批註功能:

如果有它列在那裏打擾你,你可以使用一個@type註解來替代

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

我實際上希望堅持'@ param'註釋來保證所有函數的一致性。看起來我不得不爲這些功能使用'@type'註釋。 – light

相關問題