TIL: adding jsdoc typehints to imports

defining custom objects and using them as the typehints for imported modules in vanilla javascript

December 4, 2024
// hooks/global_utils.js

// @ts-check

/**
 * Check if a parameter is empty.
 * @param {string | null | undefined} param - param to check
 * @param {any} default_value - value to return if param is empty
 * @returns {any}
 */
function isParamEmpty(param, default_value) {
    if (param === "" || param === undefined || param === null) {
      return default_value;
    }
    return param;
  }

  module.exports = {
    isParamEmpty,
  };

// index.js

       * @typedef {Object} GlobalUtils
       * @property {(param: string | null | undefined, default_value: any) => any} isParamEmpty - Checks if a parameter is empty and returns a default value.
       */

      /** @type {GlobalUtils} */
      const { isParamEmpty } = require(`${__hooks}/global_utils.js`);