Skip to content

Latest commit

 

History

History
236 lines (201 loc) · 3.13 KB

annotation.md

File metadata and controls

236 lines (201 loc) · 3.13 KB

For more examples visit compiler annotation

Usage

/**
 * @param {type} a
 * @return {type}
 */
function foo(a) {
	return a;
}
var b = /** @type {type} */ (a);
foo(/** @type {type} */ (a));
/**
 * @type {type}
 */
Foo.prototype.a;
/**
 * @type {*} Whatever
 */
Foo.prototype.a;

/**
 * @type {string|number|undefined} String or number or undefined
 */
Foo.prototype.b;

/**
 * @type {?string} String or null
 */
Foo.prototype.c;
/**
 * @param {!string} a Required parameter
 * @param {string=} opt_b Optional parameter
 */
Foo.prototype.bar = function(a, opt_b) {

}

Primitive types

/** @type {number} */
/** @type {string} */
/** @type {boolean} */
/** @type {undefined} */
/** @type {null} */

Const

/**
 * @const
 * @type {number}
 */
Foo.A = 1;

/**
 * @const
 * @type {string}
 */
Foo.B = 'text';

Array

/**
 * @param {Array}
 */

/**
 * @param {Array.<*>} Array of unknown/whatever items
 * @param {Array.<string>} Items in array are strings
 * @param {Array.<Object>} Items in array are Objects
 */

/**
 * @param {Array.<Array.<string>>} Items in array are Arrays of strings
 */

Object

/**
 * @param {Object}
 */

/**
 * @param {Object.<number, string>} Object with number keys and string values
 */

Function

/**
 * @param {Function}
 */

/**
 * @param {function(): boolean} Param is a function which return boolean
 * @param {function(number): boolean} Function with number as parameter and return boolean
 */

Html

/** @type {Element} */
/** @type {Document} */
/** @type {DocumentFragment} */
/** @type {Node} */
/** @type {NodeList} */
/** @type {Window} */

/** @type {HTMLAnchorElement} */
/** @type {HTMLDocument} */
/** @type {HTMLElement} */
/** @type {HTMLFormElement} */
/** @type {HTMLImageElement} */

Typedef

/**
 * @typedef {{
 *      id: number,
 *      direction: string
 *  }}
 */
Foo.NAVIGATE_INFO;

/**
 * @type {Foo.NAVIGATE_INFO}
 */
Foo.prototype.info;
/**
 * Function returning "typedef". Property "style" can be string or null.
 *
 * @return {{link: string, href: string, style: (string|null)}}
 */
Foo.prototype.getLink = function() {
	// ...
	return {
		link: 'Link',
		href: 'http://...',
		style: null
	};
}

Enum

/**
 * @enum {string}
 */
Foo.Api = {
	GET: '/get',
	PUT: '/put'
}

/**
 * @enum {number}
 */
Foo.Direction= {
	UP: 1,
	DOWN: 2
}

VarArgs

/**
 * @param {...string} var_args
 */
Foo.prototype.insertAll = function(var_args) {
	// arguments, arguments.length
};

OOP

/**
 * @interface
 */
Foo = function() {};

/**
 * @param {number} a
 */
Foo.prototype.bar = function(a) {};
/**
 * @constructor
 * @implements {Foo}
 */
app.Some = () {

};

/**
 * @param {number} a
 */
app.Some.prototype.bar = function(a) {

};
/**
 * @constructor
 * @extends {app.Some}
 */
app.Other = () {
	goog.base(this);
};