google / closure-compiler

A JavaScript checker and optimizer.
https://developers.google.com/closure/compiler/
Apache License 2.0
7.4k stars 1.15k forks source link

Overrided interface properties does not inherit type information #404

Open liolick opened 10 years ago

liolick commented 10 years ago

I have followed code:

/**
 * @interface
 */
var Itf = function(){};

/** @type {boolean} */
Itf.prototype.prop;

/**
 * @constructor
 * @implements  {Itf}
 */
var Cls = function(){}

/** @override */
Cls.prototype.prop;

Compiling it with "--compilation_level ADVANCED_OPTIMIZATIONS --jscomp_warning reportUnknownTypes --warning_level VERBOSE" I get a warning:

test.js:17: WARNING - could not determine the type of this expression
Cls.prototype.prop;
^

If I add @type {boolean} to Cls.prototype.prop all is fine. It seems compiler is unable to infer type from Itf.prototype.prop.

Initializing overrided property resolves a problem, but in my case it should be uninitialized (it's type actually {(boolean|undefined)}).

See discussion: https://groups.google.com/forum/#!topic/closure-compiler-discuss/gdKj25kAWeg

dimvar commented 10 years ago

Thanks for the report. We will fix this in the new type checker.

dimvar commented 10 years ago

Btw, if you want the type to be (boolean|undefined), you should declare that type on the interface instead of just boolean.

liolick commented 10 years ago

Thanks, Dimitris! Yes, it's (boolean|undefined) on interface, I just shortened an example here.

Are there any plans on new type checker? Can we try it on latest (v20140407) compiler via Java API? What is a suitable workaround for a while?

dimvar commented 10 years ago

You can try it by setting this option https://github.com/google/closure-compiler/blob/4b3d5e471b410e552ca17d7bc39dcb5d4cdf21f7/src/com/google/javascript/jscomp/CompilerOptions.java#L1760

But it's still experimental and not stable. We hope to land it in the next 2-3 months.