Closure Compiler警告“坏类型注释。未知类型...“当Ecmascript 6类扩展时 [英] Closure Compiler warns "Bad type annotation. Unknown type …" when Ecmascript 6 class is extended

查看:326
本文介绍了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提示是在警告消息中,但如果您不熟悉 Closure Compiler的注释检查


Closure编译器可以使用有关JavaScript变量的数据类型信息,优化和警告。 JavaScript因此无法声明类型。



因为JavaScript没有声明变量类型的语法,所以必须在代码中使用注释来指定数据








$ b

b

Closure Compiler报告在 Dog.js 中无法识别type Animal 。这是因为你正在导出一个未命名的类表达式: export default class



中使用标记 Animal 时,名称( export default class Animal )和Closure Compiler可以识别标记 c> Dog.js



你也可以给你的类一个JSDoc,标记为 @constructor

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


I'm getting a warning for every Ecmascript 6 class that inherits from another class when compiling with Closure Compiler:

I've dumbed things down as much as possible and still get the warning:

/src/main/js/com/tm/dev/Dog.js: WARNING - Bad type annotation. Unknown type module$$src$main$js$com$tm$dev$Animal.default

The compiled code does run correctly. (I've tried a number of annotations which only made things worse.) Anyone know what's expected here?

Animal.js:

export default class{
    constructor(){
        this.legs = [];
    }
    addLeg(legId){
        this.legs.push( legId );
    }
}

Dog.js:

import Animal from './Animal';

export default class extends Animal {
    constructor(){
        super();
        [1,2,3,4].forEach(leg=>this.addLeg(leg));
        console.log( 'Legs: ' + this.legs.toString() );
    }
}

解决方案

A hint is in the warning message, though it would obviously be confusing if you're not familiar with Closure Compiler's annotation inspection.

The Closure Compiler can use data type information about JavaScript variables to provide enhanced optimization and warnings. JavaScript, however, has no way to declare types.

Because JavaScript has no syntax for declaring the type of a variable, you must use comments in the code to specify the data type.

(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类扩展时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆