-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Annotating Types
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.
For more information about specific types that can be used in @type annotations see: Types in the Closure Type System
/** @type {string} */
let x = 'fruit';
or the more concise inline var declaration:
let /** string */ x = 'fruit';
Variables declared through destructuring can only be typed with an inline var declaration:
const {/** string */ x, /** number */ y} = obj;
/**
* @param {number} x
* @return {string}
*/
function f(x) { return x + ' apples'; }
or the more concise inline function declaration:
function /** string */ f(/** number */ x) {return x + ' apples'}
or as a complete type:
/** @type {function(number):string} */
function f(x) {return x + ' apples'}
/** @type {string} */
x.prop = 'fruit';
or
let x = {
/** @type {string} */
prop : 'fruit'
};
try {
...
} catch (/** @type {string} */ e) {
...
}
Type cast precede a parenthesized expression.
let x = /** @type {string} */ (fruit);
Complex types (including unions, and record types) can be aliased for convenience and maintainability using a typedef. These annotations can be long, but can be split over multiple lines for readability.
/**
* @typedef {{
* foo:string,
* bar:number,
* foobar: (number|string)
* }}
*/
let MyType;
/** @param {MyType} bar */
function foo(bar) {}