Skip to content

"About Types" page based on @type #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
276 changes: 276 additions & 0 deletions about-types.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
<!DOCTYPE html>
<!-- THIS IS A GENERATED FILE. DO NOT EDIT. -->
<html lang="en">

<head>
<meta charset="utf-8">
<meta name="description" content="The JSDoc type system.">
<title>Use JSDoc: Types</title>
<link rel="stylesheet" href="styles/usejsdoc.css">
<link rel="stylesheet" href="styles/prettify.css">
<link rel="stylesheet" href="styles/css3-github-ribbon.css">
<script src="scripts/prettify.js"></script>
<!--[if lt IE 9]>
<script src="scripts/html5shiv.min.js"></script>
<script src="scripts/html5shiv-printshiv.min.js"></script>
<![endif]-->
</head>

<body>
<header>
<a href="./index.html">@use JSDoc</a>
</header>
<article>
<h1>Types</h1>
<p>JSDoc allows you to specify type expressions identifying the type of value that a symbol may contain, a parameter type, or the type of a value returned by a
function. These type expressions are specified with the <a href="tags-type.html"><code>@type</code> tag</a>, the <a href="tags-param.html"><code>@param</code> tag</a>,
and many others.</p>
<h2 id="overview">Overview</h2>
<p>A type expression can include the JSDoc namepath to a symbol (for example, <code>myNamespace.MyClass</code>); a built-in JavaScript type (for example, <code>string</code>);
or a combination of these. You can use any
<a href="https://github.com/google/closure-compiler/wiki/Annotating-JavaScript-for-the-Closure-Compiler#type-expressions">Google Closure Compiler type expression</a>,
as well as several other formats that are specific to JSDoc.</p>
<p>If JSDoc determines that a type expression is invalid, it will display an error message. To force JSDoc to stop running when a type expression is invalid, use
the <code>--pedantic</code> option.</p>
<p><strong>Note</strong>: 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.
</p>
<p>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, <code>@type {MyClass}</code> will link to the MyClass documentation if that symbol has been documented.
</p>
<table id="jsdoc-types" name="jsdoc-types">
<tr>
<th>Type name</th>
<th>Syntax examples</th>
<th>Description</th>
</tr>
<tr>
<td>Symbol name (name expression)</td>
<td>
<figure>
<pre class="prettyprint"><code>{boolean}
{myNamespace.MyClass}
</code></pre>
</figure>
</td>
<td>
<p>
Specifies the name of a symbol. If you have documented the symbol, JSDoc creates a link to the documentation for that symbol.
</p>
</td>
</tr>
<tr>
<td>
Multiple types (type union)
</td>
<td>
<figure>
<figcaption>This can be a number or a boolean.</figcaption>
<pre class="prettyprint"><code>{(number|boolean)}
</code></pre>
</figure>
</td>
<td>
<p>
This means a value can have one of several types, with the entire list of types enclosed in parentheses and separated by <code>|</code>. The parentheses
are suggested for Closure Compiler, but may be omitted in JSDoc.
</p>
</td>
</tr>
<tr>
<td>
Arrays and objects (type applications and record types)
</td>
<td>
<figure>
<figcaption>An array of MyClass instances.</figcaption>
<pre class="prettyprint"><code>{Array.&lt;MyClass&gt;}
// or:
{MyClass[]}
</code></pre>
</figure>
<figure>
<figcaption>An object with string keys and number values:</figcaption>
<pre class="prettyprint"><code>{Object.&lt;string, number&gt;}
</code></pre>
</figure>
<figure>
<figcaption>An object called &#39;myObj&#39; with properties &#39;a&#39; (a number), &#39;b&#39; (a string), and &#39;c&#39; (any type).</figcaption>
<pre class="prettyprint"><code>&#123;{a: number, b: string, c}} myObj
// or:
{Object} myObj
{number} myObj.a
{string} myObj.b
{&ast;} myObj.c
</code></pre>
</figure>
</td>
<td>
<p>
JSDoc supports Closure Compiler&#39;s syntax for defining array and object types.
<p>
<p>
You can also indicate an array by appending <code>[]</code> to the type that is contained in the array. For example, the expression <code>string[]</code> indicates an array of strings.
</p>
<p>
For objects that have a known set of properties, you can use Closure Compiler&#39;s syntax for documenting record types. As an alternative, you can document each
property individually, which enables you to provide more detailed information about each property.
</p>
</td>
</tr>
<tr>
<td>
Nullable type
</td>
<td>
<figure>
<figcaption>A number or null.</figcaption>
<pre class="prettyprint"><code>{?number}
</code></pre>
</figure>
</td>
<td>
<p>
This indicates that the type is either the specified type, or <code>null</code>.
</p>
</td>
</tr>
<tr>
<td>
Non-nullable type
</td>
<td>
<figure>
<figcaption>A number, but never null.</figcaption>
<pre class="prettyprint"><code>{!number}
</code></pre>
</figure>
</td>
<td>
<p>
Indicates that the value is of the specified type, but cannot be <code>null</code>.
</p>
</td>
</tr>
<tr>
<td>
Variable number of that type
</td>
<td>
<figure>
<figcaption>This function accepts a variable number of numeric parameters.</figcaption>
<pre class="prettyprint"><code>@param {...number} num
</code></pre>
</figure>
</td>
<td>
<p>
Indicates that the function accepts a variable number of parameters, and specifies a type for the parameters. For example:
</p>
<figure>
<pre class="prettyprint"><code>/&ast;&ast;
&ast; Returns the sum of all numbers passed to the function.
&ast; @param {...number} num A positive or negative number
&ast;/
function sum(num) {
var i=0, n=arguments.length, t=0;
for (; i&lt;n; i++) {
t += arguments[i];
}
return t;
}
</code></pre>
</figure>
</td>
</tr>
<tr>
<td>
Optional parameter
</td>
<td>
<figure>
<figcaption>An optional parameter named foo.</figcaption>
<pre class="prettyprint"><code>@param {number} [foo]
// or:
@param {number=} foo
</code></pre>
</figure>
<figure>
<figcaption>An optional parameter foo with default value 1.</figcaption>
<pre class="prettyprint"><code>@param {number} [foo=1]
</code></pre>
</figure>
</td>
<td>
<p>
Indicates that the parameter is optional. When using JSDoc&#39;s syntax for optional parameters, you can also indicate the value that will be used if a parameter
is omitted.
</p>
</td>
</tr>
<tr>
<td>
Callbacks
</td>
<td>
<figure>
<pre class="prettyprint"><code>/&ast;&ast;
&ast; @callback myCallback
&ast; @param {number} x - ...
&ast;/

/&ast;&ast; @type {myCallback} &ast;/
var cb;
</code></pre>
</figure>
</td>
<td>
<p>
Document a callback using the <a href="tags-callback.html"><code>@callback</code></a> tag. The syntax is identical to the <code>@typedef</code> tag,
except that a callback&#39;s type is always
<code>function</code>.
</p>
</td>
</tr>
<tr>
<td>
Type definitions
</td>
<td>
<figure>
<figcaption>Documenting a type with properties &#39;id&#39;, &#39;name&#39;, &#39;age&#39;.</figcaption>
<pre class="prettyprint"><code>/&ast;&ast;
&ast; @typedef PropertiesHash
&ast; @type {object}
&ast; @property {string} id - an ID.
&ast; @property {string} name - your name.
&ast; @property {number} age - your age.
&ast;/

/&ast;&ast; @type {PropertiesHash} &ast;/
var props;
</code></pre>
</figure>
</td>
<td>
<p>
You can document complex types using the <a href="tags-typedef.html">@typedef</a> tag, then refer to the type definition elsewhere in your documentation.
</p>
</td>
</tr>
</table>
</article>
<footer>
<a class="license-badge" href="http://creativecommons.org/licenses/by-sa/3.0/">
<img alt="Creative Commons License" class="license-badge" src="images/cc-by-sa.svg" width="80" height="15" /></a>
<br> Copyright &#169; 2011-2017 the
<a href="https://github.com/jsdoc3/jsdoc3.github.com/contributors">contributors</a> to the JSDoc 3 documentation project.
<br> This website is <a href="https://github.com/jsdoc3/jsdoc3.github.com">open source</a> and is licensed under the <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/">
Creative Commons Attribution-ShareAlike 3.0 Unported License</a>.
</footer>
<script type="text/javascript">
prettyPrint();
</script>
</body>

</html>
Loading