27
loading...
This website collects cookies to deliver better user experience
let foo = 42; // foo is now a number
foo = 'bar'; // foo is now a string
foo = true; // foo is now a boolean
type
of of your code.let foo = 42;
it will show you methods of a number:id
, createdOn
, etc.@param
, @type
and @typedef
block tags.The @param
tag provides the name, type, and description of a function parameter.
The @param
tag requires you to specify the name of the parameter you are documenting. You can also include the parameter's type, enclosed in curly brackets, and a description of the parameter.
/**
* @param {string} somebody
*/
function sayHello(somebody) {
alert('Hello ' + somebody);
}
sayHello
: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.
/** @type {Array} */
var foo;
foo.
will load all Array
's properties and methods:The @typedef
tag is useful for documenting custom types, particularly if you wish to refer to them repeatedly. These types can then be used within other tags expecting a type, such as @type
or @param
.
// src/toast.js
/**
* @typedef {Object} Toast
* @property {string} id
* @property {boolean} closed - Indicates whether user has close the toast.
* @property {Date} generatedOn - Indicates when the toast was generated.
* @property {string} message - toast content.
* @property {"warn" | "info"} type - Indicates type of toast.
* Also useful to show different icons.
*/
/**
* A function for showing toast
* @param {Toast} toast - {@link toast} object
* containing all components of the toast.
*/
export function showToast(toast) {}
@typedef
tagObject
. You can also create simpler custom type using primitive date types, for example string
or number
.Toast
Toast
is going to be an Object
, in rest of the comments, we defined what are it's properties
going to be using @property
tag. You can learn more about @property
tag here.showToast
, VS code will do it's magic:Toast
s in different files and calling showToast
from there. You can export and import showToast
in other files, but what about Toast
type definition?// src/home.js
import { showToast } from "./toast";
/**
* @returns {import("./toast").Toast[]}
*/
function getToasts() {}
const allToasts = getToasts();
allToasts.forEach((toast) => {
showToast(toast);
});
Toast
type definition:/**
* @returns {import("./toast").Toast[]}
*/
@typedef
at https://jsdoc.app/tags-typedef.html.@param
, @type
and @typedef
can help us to achieve maximum out of VS Code's intellisense and code faster without getting into un-wanted issues.