本文介绍了Closure Compiler警告“坏类型注释。未知类型...“当Ecmascript 6类扩展时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个警告,每个Ecmascript 6类继承自另一个类当使用Closure Compiler编译时:



我已经尽可能多地仍然会收到警告:

  /src/main/js/com/tm/dev/Dog.js:警告 - 错误类型注释。未知类型模块$$ src $ main $ js $ com $ tm $ dev $ Animal.default 

编译代码运行正常。



Animal.js:

 导出默认类{
constructor(){
this.legs = [];
}
addLeg(legId){
this.legs.push(legId);
}
}

Dog.js:

  import Animal从'./Animal'; 

导出默认类extends Animal {
constructor(){
super();
[1,2,3,4] .forEach(leg => this.addLeg(leg));
console.log('Legs:'+ this.legs.toString());
}
}


解决方案

A提示是在警告消息中,但如果您不熟悉。

(The following is untested.)

Closure Compiler is reporting that in Dog.js it does not recognise the "type" Animal. This is because you are exporting an unnamed class expression: export default class.

So you could give your class a name (export default class Animal) and Closure Compiler may recognise the token Animal when it is consumed in Dog.js.

And you can also give your class a JSDoc that marks it as a @constructor:

/**
 * Animal.
 * @constructor
 */
export default class Animal {}

这篇关于Closure Compiler警告“坏类型注释。未知类型...“当Ecmascript 6类扩展时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 18:41