-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathabout-types.html
276 lines (271 loc) · 9.67 KB
/
about-types.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
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.<MyClass>}
// or:
{MyClass[]}
</code></pre>
</figure>
<figure>
<figcaption>An object with string keys and number values:</figcaption>
<pre class="prettyprint"><code>{Object.<string, number>}
</code></pre>
</figure>
<figure>
<figcaption>An object called 'myObj' with properties 'a' (a number), 'b' (a string), and 'c' (any type).</figcaption>
<pre class="prettyprint"><code>{{a: number, b: string, c}} myObj
// or:
{Object} myObj
{number} myObj.a
{string} myObj.b
{*} myObj.c
</code></pre>
</figure>
</td>
<td>
<p>
JSDoc supports Closure Compiler'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'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>/**
* Returns the sum of all numbers passed to the function.
* @param {...number} num A positive or negative number
*/
function sum(num) {
var i=0, n=arguments.length, t=0;
for (; i<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'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>/**
* @callback myCallback
* @param {number} x - ...
*/
/** @type {myCallback} */
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's type is always
<code>function</code>.
</p>
</td>
</tr>
<tr>
<td>
Type definitions
</td>
<td>
<figure>
<figcaption>Documenting a type with properties 'id', 'name', 'age'.</figcaption>
<pre class="prettyprint"><code>/**
* @typedef PropertiesHash
* @type {object}
* @property {string} id - an ID.
* @property {string} name - your name.
* @property {number} age - your age.
*/
/** @type {PropertiesHash} */
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 © 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>