Javascript 闭包编译器外部-引用复杂函数
如何使用Google闭包编译器引用复杂函数类型而不是构造函数的实例 externs.js-用于CoolLibrary.matchSomething的externsJavascript 闭包编译器外部-引用复杂函数,javascript,types,module,google-closure-compiler,Javascript,Types,Module,Google Closure Compiler,如何使用Google闭包编译器引用复杂函数类型而不是构造函数的实例 externs.js-用于CoolLibrary.matchSomething的externs /** @externs */ /** @const */ const CoolLibrary = {}; /** * @param {!Object} object The object to inspect. * @param {!Object} source The object of property values t
/** @externs */
/** @const */
const CoolLibrary = {};
/**
* @param {!Object} object The object to inspect.
* @param {!Object} source The object of property values to match.
* @param {!function(!Object): !Object} customizer The function to customize
* comparisons.
* @return {boolean} Returns `true` if `object` is a match, else `false`.
* @constructor
*/
CoolLibrary.matchSomething = function(object, source, customizer) {};
/**
* @param {string} src
* @return {!Object}
*/
function require(src) {}
foo.js-应用程序代码
goog.module('foo');
const isMatchWith = /** @type {!CoolLibrary.matchSomething} */ (require('coollibrary.matchsomething'));
const foo = isMatchWith({}, {}, (val) => {});
我这样调用它:
java -jar closure-compiler-v20161201.jar --js=foo.js --externs=externs.js --new_type_inf
这里有一个可运行的版本
它的错误包括:
foo.js:3: WARNING - Bad type annotation. Unknown type Lodash.isMatchWith
const isMatchWith = /** @type {!Lodash.isMatchWith} */ (require('lodash.ismatchwith'));
^
0 error(s), 1 warning(s), 72.7% typed
如果我使用@typedef
,它会工作,但会丢失大部分信息。有没有比下面使用typedef更好的方法来添加类型信息
/** @typedef {function(!Object, !Object, function(!Object):!Object):boolean} */
CoolLibrary.matchSomething;
函数定义不是类型名。如果将函数导入多个位置,可以使用typedef防止重新键入此数据。但是,如果您只在一个位置导入信息,那么typedef就太过分了 对于单个导入,只需在调用
require
时复制类型转换中的函数注释即可
const isMatchWith =
/** @type {function(!Object, !Object, function(!Object):!Object):boolean} */
(require('lodash.ismatchwith'));
当使用模块绑定时,编译器会为您处理这些情况,但这要求所有源文件都与编译器兼容,并作为编译的一部分提供。目前,对于外部库,这是不可能的