Add easy type checking with JSDOC

Preparing an example

For this article, let's say you have a function that concatenates the first name and last name

const getFullName = (firstName, lastName) => {
  return `${firstName} + ${lastName}`;
};

Now, let's say you want to catch bugs early by early detection of type errors.

Type checking

We can do this by adding JSDOC

/**
 *
 * @param {string} firstName
 * @param {string} lastName
 */
const getFullName = (firstName, lastName) => {
  return `${firstName} + ${lastName}`;
};

Finally, to add type checking, we can add ts check

// @ts-check

/**
 *
 * @param {string} firstName
 * @param {string} lastName
 */
const getFullName = (firstName, lastName) => {
  return `${firstName} + ${lastName}`;
};