Skip to content

Instantly share code, notes, and snippets.

@mgiuffrida
Last active November 13, 2015 21:58
Show Gist options
  • Save mgiuffrida/6037d59fe683feaacb2d to your computer and use it in GitHub Desktop.
Save mgiuffrida/6037d59fe683feaacb2d to your computer and use it in GitHub Desktop.
Closure gets confused about property types when not annotated
/** @interface */
function Gum() {}
Gum.prototype = {
/** @return {boolean} */
isMinty: function() { return true; }
};
/** @constructor */
function Candy() {}
Candy.prototype = {
/** @return {string} */
flavor: function() { return 'cherry'; }
};
/** @constructor */
function Thing() { }
Thing.prototype = {
/** @param {Gum} gum */
setGum: function(gum) {
/** @private */
this.gum_ = gum; // Missing type in annotation. Closure seems to infer
// the type, but on closer expection it seems to treat
// it as a union of prototypes, somehow.
},
chew: function() {
if (this.gum_.isMinty()) // Doesn't warn
console.log('yummy');
if (this.gum_.flavor() == 'cherry') // Doesn't warn, but is a TypeError
console.log('gross');
if (this.gum_.nonExistentFn() == 'cherry') // Does warn
console.log('gross');
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment