Closure Compiler warns "Bad type annotation. Unknown type …" when Ecmascript 6 class is extended -
i'm getting warning every ecmascript 6 class inherits class when compiling closure compiler:
i've dumbed things down as possible , still 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 run correctly. (i've tried number of annotations made things worse.) know what's expected here?
animal.js:
export default class{ constructor(){ this.legs = []; } addleg(legid){ this.legs.push( legid ); } }
dog.js:
import animal './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 in warning message, though confusing if you're not familiar closure compiler's annotation inspection.
the closure compiler can use data type information javascript variables provide enhanced optimization , warnings. javascript, however, has no way declare types.
because javascript has no syntax declaring type of variable, must use comments in code specify data type.
(the following untested.)
closure compiler reporting in dog.js
not recognise "type" animal
. because exporting unnamed class expression: export default class
.
so give class name (export default class animal
) , closure compiler may recognise token animal
when consumed in dog.js
.
and can give class jsdoc marks @constructor
:
/** * animal. * @constructor */ export default class animal {}