Closure Compiler externs - 引用复杂函数
Closure Compiler externs - Referencing a complex function
如何使用 Google 闭包编译器引用复杂函数类型而不是构造函数的实例?
externs.js - CoolLibrary.matchSomething
的外部人员
/** @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'));
编译器会在使用模块捆绑时为您处理这些情况,但这需要所有源文件与编译器兼容并作为编译的一部分提供。这对于外部库目前是不可能的。
如何使用 Google 闭包编译器引用复杂函数类型而不是构造函数的实例?
externs.js - CoolLibrary.matchSomething
的外部人员/** @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'));
编译器会在使用模块捆绑时为您处理这些情况,但这需要所有源文件与编译器兼容并作为编译的一部分提供。这对于外部库目前是不可能的。