You can import JSDoc types to use in your JavaScript code.
Apply a JSDoc type to a parameter to enable type checking and autocomplete.
You can skip this step if you want to use a type defined by Wix.
To define and export a type using JSDoc:
Use JSDoc tags and definitions to define a type. For example:
/**
* @typedef {object} employee
* @property {string} name Employee's name.
* @property {string} seniority The number of years the employee has worked at the company.
* @property {number} hours The number of hours the employee works a week.
*/
You can expose your type to your site's files as either a global type or a restricted type:
export {};
below your JSDoc declaration. For example:
/**
* @typedef {object} employee
* @property {string} name Employee's name.
* @property {string} seniority The number of years the employee has worked at the company.
* @property {number} hours The number of hours the employee works a week.
*/
export {};
The way that you import the JSDoc type depends on where and how the type is exposed:
Use the type without importing it. For example:
/**
* @param {employee} employee
*/
export function salaryCalculator(employee) {
return `Salary for ${employee.name}: ${employee.hours * employee.seniority * 100}`;
}
Import your type using import('<file-path.js>').<type-def-name>
directly in the JSDoc's tag type declaration. For example:
/**
* @param {import('backend/myJSDocFile.js).employee} employee
*/
export function salaryCalculator(employee) {
return `Salary for ${employee.name}: ${employee.hours * employee.seniority * 100}`;
}
You can use Wix editor elements directly in JSDoc types without importing them. Define the JSDoc data type using $w.Element
. For example:
/** @param {$w.Text} parameterName */
By default, the file for backend event handlers, events.js
, doesn't know the type of the parameter you are passing to a function.
To tell your events.js
file the parameter's type:
EventObjectName
. You can find it in the method declaration in API reference./** @param {import('api-module-name').Events.<EventObjectName>} parameterName */
onBookingCreated
To add JSDoc to wix-bookings.v2
event, onBookingCreated()
:
onBookingCreated
. Find the EventObjectName
.events.js
file:/** @param {import('api-module-name').Events.BookingCreated} event */
/** @param {import('api-module-name').Events.BookingCreated} event */
export function wixBookings_onBookingCreated(event) {
// Handle your event
}
By default, the routers.js file doesn’t support autocomplete or type checking for WixRouterRequest objects passed to router functions.
To support autocomplete or type-checking in a routers.js
file, add the following JSDoc annotation above your router functions:
/** @param {import('wix-router').WixRouterRequest} parameterName */