cheatsheets/jsdoc.md

152 lines
2.5 KiB
Markdown
Raw Permalink Normal View History

2016-09-26 09:56:33 +00:00
---
title: Jsdoc
category: JavaScript
2020-07-04 13:33:09 +00:00
updated: 2020-06-23
2017-10-29 12:04:52 +00:00
weight: -1
2016-09-26 09:56:33 +00:00
---
2017-10-29 12:04:52 +00:00
### Functions
2016-09-26 09:56:33 +00:00
```js
2017-10-29 15:49:18 +00:00
/**
* This is a function.
*
2016-09-26 09:56:33 +00:00
* @param {string} n - A string param
2021-09-27 01:37:40 +00:00
* @param {string} [o] - A optional string param
* @param {string} [d=DefaultValue] - A optional string param
2016-09-26 09:56:33 +00:00
* @return {string} A good string
*
2017-10-29 15:49:18 +00:00
* @example
2016-09-26 09:56:33 +00:00
*
2017-10-29 15:49:18 +00:00
* foo('hello')
2016-09-26 09:56:33 +00:00
*/
2021-09-27 01:37:40 +00:00
function foo(n, o, d) {
return n
}
2016-09-26 09:56:33 +00:00
```
2020-08-03 12:27:34 +00:00
See: <https://jsdoc.app/index.html>
2016-09-26 09:56:33 +00:00
2017-10-29 12:04:52 +00:00
### Types
2016-09-26 09:56:33 +00:00
| Type | Description |
| ------------------------------- | ------------------------------------- |
| `@param {string=} n` | Optional |
| `@param {string} [n]` | Optional |
| `@param {(string|number)} n` | Multiple types |
| `@param {*} n` | Any type |
| `@param {...string} n` | Repeatable arguments |
| `@param {string} [n="hi"]` | Optional with default |
| `@param {string[]} n` | Array of strings |
| `@return {Promise<string[]>} n` | Promise fulfilled by array of strings |
2016-09-26 09:56:33 +00:00
2020-08-03 12:27:34 +00:00
See: <https://jsdoc.app/tags-type.html>
2016-09-26 09:56:33 +00:00
2017-10-29 12:04:52 +00:00
### Variables
2016-09-26 09:56:33 +00:00
```js
/**
* @type {number}
*/
var FOO = 1
2017-10-29 12:04:52 +00:00
```
2016-09-26 09:56:33 +00:00
2017-10-29 12:04:52 +00:00
```js
2016-09-26 09:56:33 +00:00
/**
* @const {number}
*/
const FOO = 1
```
2017-10-29 15:56:20 +00:00
### Typedef
2016-09-26 09:56:33 +00:00
```js
/**
* A song
* @typedef {Object} Song
* @property {string} title - The title
* @property {string} artist - The artist
* @property {number} year - The year
*/
2017-10-29 12:04:52 +00:00
```
2016-09-26 09:56:33 +00:00
2017-10-29 12:04:52 +00:00
```js
2016-09-26 09:56:33 +00:00
/**
* Plays a song
* @param {Song} song - The {@link Song} to be played
*/
function play(song) {}
2016-09-26 09:56:33 +00:00
```
2020-08-03 12:27:34 +00:00
See: <https://jsdoc.app/tags-typedef.html>
2016-09-26 09:56:33 +00:00
2019-10-01 05:29:36 +00:00
### Typedef Shorthand
{% raw %}
2019-10-01 05:29:36 +00:00
```js
/**
* A song
* @typedef {{title: string, artist: string, year: number}} Song
*/
```
{% endraw %}
2019-10-01 05:29:36 +00:00
```js
/**
* Plays a song
* @param {Song} song - The {@link Song} to be played
*/
function play(song) {}
2019-10-01 05:29:36 +00:00
```
2020-08-03 12:27:34 +00:00
See: <https://jsdoc.app/tags-typedef.html>
2019-10-01 05:29:36 +00:00
### Importing types
```js
/**
* @typedef {import('./Foo').default} Bar
*/
/**
* @param {Bar} x
*/
function test(x) {}
```
This syntax is [TypeScript-specific](https://github.com/Microsoft/TypeScript/wiki/JsDoc-support-in-JavaScript#import-types).
2018-05-20 12:45:28 +00:00
### Other keywords
2017-10-29 15:49:18 +00:00
```js
/**
* @throws {FooException}
* @async
2017-10-29 15:49:18 +00:00
* @private
* @deprecated
* @see
*
* @function
* @class
*/
```
See the full list: <https://jsdoc.app/index.html#block-tags>
2017-10-29 12:04:52 +00:00
### Renaming
2016-09-26 09:56:33 +00:00
```js
/**
2016-09-26 09:56:33 +00:00
* @alias Foo.bar
* @name Foo.bar
*/
```
2020-08-03 12:27:34 +00:00
Prefer `alias` over `name`. See: <https://jsdoc.app/tags-alias.html>