When using the google closure compiler, is it necessary to annotate properties that are set by constructor parameters? e.g.
/**
* @constructor
* @param {string} bar
* @param {number} barPrivate
*/
var Foo = function(bar, barPrivate) {
this.bar = bar;
this.barPrivate_ = barPrivate;
};
If I write this, is the compiler smart enough to figure out that:
bar
property has type {string}
?barPrivate
property has type {number}
?barPrivate
property is @private
?Or do I need to be more explicit?
/**
* @constructor
* @param {string} bar
* @param {number} barPrivate
*/
var Foo = function(bar, barPrivate) {
/** @type {string} */
this.bar = bar;
/** @private {number} */
this.barPrivate_ = barPrivate;
};
The latter form seems to be less "DRY", but it's what we've been using to be on the safe side. I haven't found any documentation on this either way...
After doing some minimal exploration the AST with visibility declarations (but no type declarations) is the same as the AST when type declarations are included... To me this means that visibility declarations are probably necessary but type declarations aren't?
I would have to run some tests to be certain but here is my understanding:
and 2. yes, the compiler knows bar
is type string
and barPrivate
is type number
. But only for the duration of the constructor call. It won't enforce that this.bar
must only be a string in later code.
no, compiler will only enforce @private
access when you provide the annotation as in your explicit example.
Some of these types of checks are more robust with the new type inference (NTI) but perhaps not these specific cases.
Try to see what the compiler does with or without those annotations when you assign a different type to the property in another method (or later on in the constructor).
The same issue comes up with whether to specify types on a local variable in a method.
edit: As Chad Killingsworth points out, in this simple example the compiler can infer that this.bar
is a string without additional annotation; but if some other type is assigned elsewhere to this.bar
then "all bets are off", I don't know what the compiler would do then.