-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Annotating Types
Michael Zhou edited this page Feb 12, 2016
·
12 revisions
The compiler recognizes @type
and function declarations (@param
, @return
, etc) annotations in two contexts: declarations and casts.
Variable and functions can be declared with either traditional declarations, or inline declarations, which are more concise.
/** @type {function(number):string} */
function f(x) {return x + ' apples'}
or the more concise inline function declaration:
function /** string */ f(/** number */ x) {return x + ' apples'}
/** @type {string} */
var x = 'fruit';
or the more concise inline var declaration:
var /** string */ x = 'fruit';
/** @type {string} */
x.prop = 'fruit';
or
var x = {
/** @type {string} */
prop : 'fruit'
};
try {
...
} catch (/** @type {string} */ e) {
...
}
Type cast precede a parenthesized expression.
var x = /** @type {string} */ (fruit);