Set up Type Checking and Autocomplete by Importing JSDoc Types

You can import JSDoc types to use in your JavaScript code.

Apply a JSDoc type to a parameter to enable type checking and autocomplete.

Important: Importing JSDoc is supported only in the code editor, not in the Wix IDE or your local IDE.

Step 1 | Define and export a type

You can skip this step if you want to use a type defined by Wix.

To define and export a type using JSDoc:

  1. Use JSDoc tags and definitions to define a type. For example:

    Copy
  2. You can expose your type to your site's files as either a global type or a restricted type:

    • Global type: You can use your defined type in any JavaScript file without further changes.
    • Restricted type: You can define different types with the same name in different files. To restrict your type, add export {}; below your JSDoc declaration. For example:
      Copy

Step 2 | Access a JSDoc type

The way that you import the JSDoc type depends on where and how the type is exposed:

Self-defined global type

Use the type without importing it. For example:

Copy

Self-defined restricted type

Import your type using import('<file-path.js>').<type-def-name> directly in the JSDoc's tag type declaration. For example:

Copy

Wix editor elements

You can use Wix editor elements directly in JSDoc types without importing them. Define the JSDoc data type using $w.Element. For example:

Copy

Backend event handlers

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:

  1. Check your event's EventObjectName. You can find it in the method declaration in API reference.
  2. Add the following code above your function declaration:
    Copy
Example: onBookingCreated

To add JSDoc to wix-bookings.v2 event, onBookingCreated():

  1. Open the API reference for onBookingCreated. Find the EventObjectName.
  2. Add the following JSDoc annotation above the event handler function in the events.js file:
    /** @param {import('api-module-name').Events.BookingCreated} event */
Copy

Router functions

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:

Copy
Was this helpful?
Yes
No