Could you just unconditionally add
/** @type {number} */
MyType.prototype.thing;
below your conditional constructor definition?
On Fri, Jan 18, 2013 at 2:15 PM, <
brook....@gmail.com> wrote:
> I'm not convinced this question was answered. The question is how to get
> the Closure compiler to stop flagging properties as not existing, when they
> do, in fact, exist.
>
> Here's a situation:
>
> var MyType;
>
> if(definePropertyWorksRight()) {
> /**
> * @constructor
> */
> MyType = function () {
> this.init();
> Object.defineProperty(this,"thing",...);
> return this;
> };
> } else {
> /**
> * @constructor
> */
> MyType = function () {
> this.init();
> this.thing = 42;
> // setThing() and getThing() written later in the file.
> return this;
> };
> }
>
> I might choose to do this if I'd like to define object properties in IE9+,
> Chrome, etc., but I can't use them in IE8 and FF3.5 because
> Object.defineProperty isn't implemented correctly. Since I'm going to make
> many of the MyType objects, I don't want to do the feature test repeatedly
> during construction for performance reasons. Variant function definition is
> the way to go with this, but the Closure compiler gets fussy. It seems like
> I ought to be able to annotate my way around this situation.
>
> MyType.prototype.init is defined later in the file (and if you try to do it
> before defining the constructor, the Closure compiler complains). I still
> see JSC_INEXISTENT_PROPERTY showing up. That means there should be an @
> syntax for indicating that this.init() is legitimate--but what is that
> syntax? (I infer this is a single-pass compiler because a double-pass
> compiler wouldn't have this issue--it would see init declared later on.)
> There's no Javascript issue here--it's purely an annotation issue with the
> Closure compiler.