Velo by Wix: Type safety your code with JSDoc
Tags: wix, velo, javascript, jsdoc
Published: 2022-02-01 15:35:02

_Built-in code checker, JSDoc annotations, and TypeScript compiler in Velo_

From time to time, I can see in the big Velo projects how a part of the page code moves to the public files. In most, it's the projects with a few hundred/thousand lines of code per page. I understand why developers do it. Also, sometimes we want to reuse some part of the code for a few site pages. It looks like a good idea to move repeated code to a public file, and reuse it.

The main problem with this pattern is that doesn't work autocomplete and ID validation of `$w()` selectors in the public files. For example, we want to move a button handler to the public file. And init it on the page code.

**public/initPage.js**

```js
// Filename: public/initPage.js

export const initPage = () => {
  const button = $w('#button1');

  button.onClick(() => { /* ... */ });
}
```

**Page code**

```js
import { initPage } from 'public/initPage.js';

$w.onReady(() => {
  // Init page code from the public file.
  initPage();
});
```

In the public files, we can see a missing type inference. There don't work hints for `$w()` selector and don't work page elements autocomplete.

**public/initPage.js**

```js
// Filename: public/initPage.js

export const initPage = () => {
  // 1. Autocomplete for ID suggestions doesn't work
  // 2. The checking of an element ID doesn't work.
  // 3. If the element with this ID doesn't exist on the page
  //    we don't have any error messages in editor.
  // 4. button mark as `any` type
  const button = $w('#button1');

  // 1. Autocomplete for button's method/properties doesn't work
  // 2. Type checking doesn't work.
  button.onClick(() => { /* ... */ });
}
```

For me, it's the main reason for don't use this pattern. The element could be removed or renamed at any time, and we don't have any editor hints, errors, or warnings to catch it. We could get a runtime error and, we should debug it with console or [site logs](https://support.wix.com/en/article/velo-about-site-monitoring).

However, this pattern is very commonly used. So, let's do it a little bit safer.

## Why does it happen?

Firstly, the public files don't design for using the `$w()` selector. Velo code checker doesn't know how we plan to use a public file. Because we can import public files to any files on any pages, also we can import a public file to the backend files, other public files, or custom web-component code.

### How Velo autocomplete works?

Velo uses a [TypeScript](https://www.typescriptlang.org/) compiler for autocomplete and code validations. Each page code file has built-in types of all elements on the current page.

>[Velo currently uses a TypeScript compiler for autocomplete and code validations](https://www.wix.com/velo/forum/tips-tutorials-examples/cannot-redeclare-block-scoped-variable-validation-error)

Page element types are generated automatically, when we add/remove any element on the page, Velo adds/removes a property for this target element in `PageElementsMap` type. The `PageElementsMap` type is unique on each page. So, each page code file has its own map of elements for autocompletion.

We to able to use this type with [JSDoc](https://jsdoc.app/) types annotation. For example, we can use a [TypeScript JSDoc syntax](https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html) to describe types.

**Page code**

```js
/**
 * @template {keyof PageElementsMap} T
 *
 * @param {T} selector
 * @param {$w.EventHandler} eventHandler
 * @returns {PageElementsMap[T]}
 */
const clickHandler = (selector, eventHandler) => {
  const element = $w(selector);

  element.onClick(eventHandler);

  return element;
}

// You can see this function has type checking for arguments and return value
clickHandler('#button1', (event) => {
  console.log(event);
});
```

If you try to use the code snippet above on your page code file, you can see that it has all type checking and autocomplete for arguments and a returned value. It's amazing, but we still can't use it on the public files, because the `PageElementsMap` type available only on the page code files.

## How can we use a JSDoc on public files?

As we can see above, the autocomplete of the `$w()` selector doesn't work on the public files because TypeScript doesn't know about the context of the public file use. We can import public files anywhere in the code. So, we should describe the types.

### Variable annotations with `@type` tag

Let's start with the simple use case. We can add variable annotations with the [`@type` tag](https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html#type).

> #### Syntax
> JSDoc it's a block comment that starts with `/**` and ends with `*/`. Inside block comment, we use a tag keyword that starts with `@` symbol after tag keyword inside curly braces `{}` we put a type.
>
> `/** @tag {type} */`

Velo provides autocomplete and syntax validation for JSDoc annotations. Just try to write the next snippet code in Velo editor without copy-pasting.

**Velo: simple example of `@type` tag**

```js
/** @type {$w.Button} */
const button = $w('#button1');
```

`$w.Button` it's a built-in type. Velo has built-in types for all page elements. You can find it here: [Wix element types](https://runkit-packages.com/14.x.x/1641656995847/corvid-types/types/pages/$w.d.ts)

The main benefits of the element types, we can use it on the public files. In the simple use case, we add the type annotations to all elements that we start to use in a public file.

**public/initPage.js**

```js
// Filename: public/initPage.js

export function initPage() {
  /** @type {$w.Button} */
  const button = $w('#button1');

  /** @type {$w.TextInput} */
  const input = $w('#input1');

  /** @type {$w.Text} */
  const text = $w('#text1');

  // your code goes here ...
}
```

Now, TypeScript understands what kind of elements we want to use. But TS still can check it.

Here, we just say to TypeScript - _"Hey TS, I know it is the button. Just trust me and work with this element as the button"_.

We solve a problem with autocomplete suggestions for elements methods and properties in the public files. But we don't solve the issue when an element is removed or renamed from the page. TypeScript compiler can check `$w()` selectors only on the page code files.

### Arguments annotation with `@param` tag

So, if we want to get autocomplete for elements and validation for `$w()` selectors, we should pass the elements explicitly from the page code to the public file as function arguments.

[`@param` tag](https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html#param-and-returns) uses the same type syntax as `@type`, but adds a parameter name.

**Syntax:** JSDoc function arguments

```js
/**
 * @param {type} name
 */
```

Let's update `initPage()` function for two arguments:

**public/initPage.js**

```js
// Filename: public/initPage.js

/**
 * @param {$w.Button} button
 * @param {$w.TextInput} input
 */
export function initPage(button, input) {
  // your code goes here ...

  button.onClick(() => { /*...*/ });

  input.onInput(() => { /*...*/ });
}
```

Now, when we start using the `initPage()` function on the page code file, we can see autocomplete list.

**Velo: autocomplete suggestion list**


![autocomplete suggestions in velo editor](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r9gf3fcqm145ip0r0ipe.jpg)

After typing the first `$` symbol, we see a list of the suggestions. We can move on the list with <kbd>↑</kbd> <kbd>↓</kbd> keys and select one with <kbd>↵ Enter</kbd> key.

Also, we can see the `initPage()` function has arguments types validation.

**Velo: type error, a function expect an `$w.TextInput` type instead `$w.Page`**


![annotation type error with jsdoc in velo editor](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tneafs7qzb695f1f50ib.jpg)

It's very cool! Now, I can sleep calmly 😀

### Interface as a function param

Suppose, we want to use more than 2 arguments in the `initPage()` function. In this case, I guess better to use an object as an argument and put elements to object property. With object argument, we don't depend on the order of params. An object has more flexibility if we want to add or remove a new element.

Here we can use an interface syntax. It's similar to CSS syntax, where we describe a key name and types inside curly braces 

`@param { { name1: type; name2: type; … } } paramName`

**public/initPage.js**

```js
// Filename: public/initPage.js

/**
 * @param {{
 * button: $w.Button;
 * input: $w.TextInput;
 * text: $w.Text;
 * box: $w.Box;
 * }} elements
 */
export function initPage({
  button,
  input,
  text,
  box,
}) {
  // your code goes here ...

  button.onClick(() => { /*...*/ });

  input.onInput(() => { /*...*/ });
}
```

We have the autocomplete for the object keys and values. Very useful.

**Velo: autocomplete and type validation**

<video controls="" height="576" loop="" preload="metadata" src="https://shoonia.site/assets/videos/jsdoc1.mp4" type="video/mp4" width="1280"></video>


## Resources

- [Official documentation for JSDoc 3](https://jsdoc.app/)
- [TypeScript: Documentation - JSDoc Reference](https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html)
- [JSDoc Cheatsheet and Type Safety Tricks](https://docs.joshuatz.com/cheatsheets/js/jsdoc/)

## Posts

- [Repeated item event handlers v2.0](https://shoonia.site/repeated-item-event-handlers-v2/)
- [Query selector for child elements](https://shoonia.site/velo-query-selector-for-child-elements/)
- [Promise Queue](https://shoonia.site/promise-queue/)