Last active
November 13, 2015 21:58
-
-
Save mgiuffrida/6037d59fe683feaacb2d to your computer and use it in GitHub Desktop.
Closure gets confused about property types when not annotated
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** @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