【问题标题】:Closure Compiler - Could not resolve type in @extends tag闭包编译器 - 无法解析 @extends 标记中的类型
【发布时间】:2016-03-19 15:04:40
【问题描述】:

当我使用 ADVANCED_OPTIMIZATIONS 进行编译时,Closure Compiler 会输出一个奇怪的警告。

你可以在Closure Compiler UI看到它

当我使用接口编译此代码时出现警告 - @interface

// ==ClosureCompiler==
// @output_file_name default.js
// @compilation_level ADVANCED_OPTIMIZATIONS
// ==/ClosureCompiler==

/**
 * @interface
 * @extends {IReused}
 */
function IRegistration() {
}

/**
 * @interface
 */
function IInitializable() {
}

/**
 * @param {IReused} initializer
 */
IInitializable.prototype.initializedBy = function (initializer) { };

/**
 * @interface
 * @extends {IOwned}
 */
function IReused() {
}


/**
 * @interface
 */
function IOwned() {
}

输出:

WARNING - Could not resolve type in @extends tag of IRegistration
function IRegistration() {
^
0 error(s), 1 warning(s)   

但是当我使用 @constructor 将所有接口更改为类时,一切正常。

// ==ClosureCompiler==
// @output_file_name default.js
// @compilation_level ADVANCED_OPTIMIZATIONS
// ==/ClosureCompiler==

/**
 * @constructor
 * @extends {IReused}
 */
function IRegistration() {
}

/**
 * @constructor
 */
function IInitializable() {
}

/**
 * @param {IReused} initializer
 */
IInitializable.prototype.initializedBy = function (initializer) { };

/**
 * @constructor
 * @extends {IOwned}
 */
function IReused() {
}


/**
 * @constructor
 */
function IOwned() {
}

当我从 initializedBy 函数中删除 IReused 参数时使用接口时,一切都按预期工作。

/**
 * @interface
 * @extends {IReused}
 */
function IRegistration() {
}
...


/**
 * @param {IReused} initializer
 */
IInitializable.prototype.initializedBy = function (initializer) { };

To ->

/**
 * @param {*} initializer
 */
IInitializable.prototype.initializedBy = function (initializer) { };

谁能解释一下我做错了什么?

【问题讨论】:

  • 命令您在不需要前向引用的地方键入。
  • 不需要前向引用是什么意思?可以举个例子吗?

标签: javascript google-closure-compiler


【解决方案1】:

问题是IReusedIOwnedIRegistration中使用后定义了。

如果你只是将 declerations 向上移动,它会工作得很好。 在编译器 UI here 中查看它。

这也是代码的样子:

// ==ClosureCompiler==
// @output_file_name default.js
// @compilation_level ADVANCED_OPTIMIZATIONS
// ==/ClosureCompiler==

/**
 * @interface
 */
function IOwned() {
}

/**
 * @interface
 * @extends {IOwned}
 */
function IReused() {
}


/**
 * @interface
 * @extends {IReused}
 */
function IRegistration() {
}

/**
 * @interface
 */
function IInitializable() {
}

/**
 * @param {IReused} initializer
 */
IInitializable.prototype.initializedBy = function (initializer) { };

【讨论】:

  • 如果无法向上移动接口的声明,您还可以从代码生成前向声明并将其提供给编译器。
  • 感谢您的评论。但是我不明白为什么只有当我使用接口而不是类时才会出现这个问题?
  • 老实说,我不知道为什么,但假设它与接口和类的实现方式有关。在编译器传递中,接口可能比类更早地被静态检查。闭包编译器 google group 或 github repo 的专家可能会给出更明智的答案。
猜你喜欢
  • 2019-10-22
  • 2012-09-12
  • 1970-01-01
  • 2013-04-05
  • 2016-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多