7.10 Type annotations

Type annotations are found on @param, @return, @this, and @type tags,and optionally on @const, @export, and any visibility tags. Typeannotations attached to JSDoc tags must always be enclosed in braces.

7.10.1 Nullability

The type system defines modifiers ! and ? for non-null and nullable,respectively. These modifiers must precede the type.

Nullability modifiers have different requirements for different types, whichfall into two broad categories:

  • Type annotations for primitives (string, number, boolean, symbol,undefined, null) and literals ({function(…): …} and {{foo:string…}}) are always non-nullable by default. Use the ? modifier tomake it nullable, but omit the redundant !.
  • Reference types (generally, anything in UpperCamelCase, includingsome.namespace.ReferenceType) refer to a class, enum, record, or typedefdefined elsewhere. Since these types may or may not be nullable, it isimpossible to tell from the name alone whether it is nullable or not. Alwaysuse explicit ? and ! modifiers for these types to prevent ambiguity atuse sites.Bad:
  1. const /** MyObject */ myObject = null; // Non-primitive types must be annotated.
  2. const /** !number */ someNum = 5; // Primitives are non-nullable by default.
  3. const /** number? */ someNullableNum = null; // ? should precede the type.
  4. const /** !{foo: string, bar: number} */ record = ...; // Already non-nullable.
  5. const /** MyTypeDef */ def = ...; // Not sure if MyTypeDef is nullable.
  6. // Not sure if object (nullable), enum (non-nullable, unless otherwise
  7. // specified), or typedef (depends on definition).
  8. const /** SomeCamelCaseName */ n = ...;

Good:

  1. const /** ?MyObject */ myObject = null;
  2. const /** number */ someNum = 5;
  3. const /** ?number */ someNullableNum = null;
  4. const /** {foo: string, bar: number} */ record = ...;
  5. const /** !MyTypeDef */ def = ...;
  6. const /** ?SomeCamelCaseName */ n = ...;

7.10.2 Type Casts

In cases where the compiler doesn't accurately infer the type of an expression,and the assertion functions ingoog.assertscannot remedy it , it is possible totighten the type by adding a type annotation comment and enclosing theexpression in parentheses. Note that the parentheses are required.

  1. /** @type {number} */ (x)

7.10.3 Template Parameter Types

Always specify template parameters. This way compiler can do a better job and itmakes it easier for readers to understand what code does.

Bad:

  1. const /** !Object */ users = {};
  2. const /** !Array */ books = [];
  3. const /** !Promise */ response = ...;

Good:

  1. const /** !Object<string, !User> */ users = {};
  2. const /** !Array<string> */ books = [];
  3. const /** !Promise<!Response> */ response = ...;
  4. const /** !Promise<undefined> */ thisPromiseReturnsNothingButParameterIsStillUseful = ...;
  5. const /** !Object<string, *> */ mapOfEverything = {};

Cases when template parameters should not be used:

  • Object is used for type hierarchy and not as map-like structure.

7.10.4 Function type expressions

Terminology Note: function type expression refers to a type annotation forfunction types with the keyword function in the annotation (see examplesbelow).

Where the function definition is given, do not use a function type expression.Specify parameter and return types with @param and @return, or with inlineannotations (see ??). This includesanonymous functions and functions defined and assigned to a const (where thefunction jsdoc appears above the whole assignment expression).

Function type expressions are needed, for example, inside @typedef, @paramor @return. Use it also for variables or properties of function type, if theyare not immediately initialized with the function definition.

  1. /** @private {function(string): string} */
  2. this.idGenerator_ = googFunctions.identity;

When using a function type expression, always specify the return typeexplicitly. Otherwise the default return type is unknown (?), which leads tostrange and unexpected behavior, and is rarely what is actually desired.

Bad - type error, but no warning given:

  1. /** @param {function()} generateNumber */
  2. function foo(generateNumber) {
  3. const /** number */ x = generateNumber(); // No compile-time type error here.
  4. }
  5. foo(() => 'clearly not a number');

Good:

  1. /**
  2. * @param {function(): *} inputFunction1 Can return any type.
  3. * @param {function(): undefined} inputFunction2 Definitely doesn't return
  4. * anything.
  5. * NOTE: the return type of `foo` itself is safely implied to be {undefined}.
  6. */
  7. function foo(inputFunction1, inputFunction2) {...}

7.10.5 Whitespace

Within a type annotation, a single space or line break is required after eachcomma or colon. Additional line breaks may be inserted to improve readability oravoid exceeding the column limit. These breaks should be chosen and indentedfollowing the applicable guidelines (e.g. ?? and??). No other whitespace is allowed in typeannotations.

Good:

  1. /** @type {function(string): number} */
  2. /** @type {{foo: number, bar: number}} */
  3. /** @type {number|string} */
  4. /** @type {!Object<string, string>} */
  5. /** @type {function(this: Object<string, string>, number): string} */
  6. /**
  7. * @type {function(
  8. * !SuperDuperReallyReallyLongTypedefThatForcesTheLineBreak,
  9. * !OtherVeryLongTypedef): string}
  10. */
  11. /**
  12. * @type {!SuperDuperReallyReallyLongTypedefThatForcesTheLineBreak|
  13. * !OtherVeryLongTypedef}
  14. */

Bad:

  1. // Only put a space after the colon
  2. /** @type {function(string) : number} */
  3. // Put spaces after colons and commas
  4. /** @type {{foo:number,bar:number}} */
  5. // No space in union types
  6. /** @type {number | string} */