Syntax
@type {typeName}
Overview
The @type tag allows you to provide a type expression identifying the type of value that a symbol may contain, or the type of value returned by a function. You can also include type expressions with many other JSDoc tags, such as the @param tag.
A type expression can include the JSDoc namepath to a symbol (for example, myNamespace.MyClass
); a
built-in JavaScript type (for example, string
); or a combination of these. You can use any
Google Closure Compiler type expression, as well as several other formats that are
specific to JSDoc.
If JSDoc determines that a type expression is invalid, it will display an error and stop running.
You can turn this error into a warning by running JSDoc with the --lenient
option.
Note: Full support for Google Closure Compiler-style type expressions is available in JSDoc 3.2 and later. Earlier versions of JSDoc included partial support for Closure Compiler type expressions.
Each type is specified by providing a type expression, using one of the formats described below.
Where appropriate, JSDoc will automatically create links to the documentation for other symbols. For
example, @type {MyClass}
will link to the MyClass documentation if that symbol has been
documented.
Type name | Syntax examples | Description |
---|---|---|
Symbol name (name expression) |
Specifies the name of a symbol. If you have documented the symbol, JSDoc creates a link to the documentation for that symbol. |
|
Multiple types (type union) |
This means a value can have one of several types, with the entire list of types enclosed in
parentheses and separated by |
|
Arrays and objects (type applications and record types) |
JSDoc supports Closure Compiler's syntax for defining array and object types.
You can also indicate an array by appending For objects that have a known set of properties, you can use Closure Compiler's syntax for documenting record types. You can document each property individually, which enables you to provide more detailed information about each property. |
|
Nullable type |
This indicates that the type is either the specified type, or |
|
Non-nullable type |
Indicates that the value is of the specified type, but cannot be |
|
Variable number of that type |
Indicates that the function accepts a variable number of parameters, and specifies a type for the parameters. For example: |
|
Optional parameter |
Indicates that the parameter is optional. When using JSDoc's syntax for optional parameters, you can also indicate the value that will be used if a parameter is omitted. |
|
Callbacks |
Document a callback using the @callback tag. The syntax is identical to the @typedef tag, except that a callback's type is always "function." |
|
Type definitions |
You can document complex types using the @typedef tag, then refer to the type definition elsewhere in your documentation. |
Examples
In many cases, you can include a type expression as part of another tag, rather than including a separate @type tag in your JSDoc comment.