[英]Regexp type for closure compiler
我目前正在為個人javascript模塊添加類型注釋,但是當我嘗試鍵入函數時,我正在考慮將regexp作為參數,但以下嘗試都沒有工作:
/** @type {function(RegExp)} */
/** @type {function(regex)} */
我只是得到:
WARNING - Bad type annotation. Unknown type regexp
我應該在聲明中使用什么類型? 謝謝。
RegExp
對象在extern es3.js中為Closure Compiler注釋。 這是一個接受RegExp
對象的示例函數。
/**
* @param {RegExp} regex A regular expression object.
* @param {string} text Text to match.
* @return {string} The first match in text.
*/
var firstMatch = function(regex, text) {
var match = regex.exec(text);
return match[1];
};
var text = 'Where art thou?';
var regex = new RegExp('^\\W*(\\w+)\\b', 'g');
var firstWord = firstMatch(regex, text);
alert(firstWord); // displays "Where"
使用@type
注釋的相同功能:
/** @type {function(RegExp, string): string} */
var firstMatch = function(regex, text) {
var match = regex.exec(text);
return match[1];
};
Closure Compiler包含默認的externs,例如es3.js,es5.js和es6.js. 除非設置了以下標志,否則通常不需要顯式指定這些外部文件 :
閉包編譯器應用程序 :-- --use_only_custom_externs=true
Closure Compiler Service UI : @exclude_default_externs true
Closure Compiler Service API : exclude_default_externs=true
在contrib / externs下還有其他外部功能 ,默認情況下不包含這些外部功能 。 可以使用--externs
標志指定這些外部。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.