Skip to content

Annotating Types

blickly edited this page Nov 26, 2014 · 12 revisions

Annotating declarations and casts

The compiler recognizes @type annotations in two contexts: declarations and casts.

Variable and functions can be declared with either traditional declarations, or inline declarations, which are more concise.

Examples

Function declarations

/** @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'}

Variable declarations

/** @type {string} */
var x = 'fruit';

or the more concise inline var declaration:

var /** string */ x = 'fruit';

Property declarations

    /** @type {string} */
    x.prop = 'fruit';

or

    var x = {
      /** @type {string} */
      prop : 'fruit'
    };

Catch declarations

try { 
  ... 
} catch (/** @type {string} */ e) {
  ...
}

Type Casts

Type cast precede a parenthesized expression.

    var x = /** @type {string} */ (fruit);
Clone this wiki locally