JSDoc typedef in a separate file

You can define types in a module (eg. typedefs.js). The module contains your JSDoc typedefs and can simply export an unused property.

// typedefs.js
/**
 * @typedef foo
 * @property {string} bar
 */

// etc.

exports.unused = {};
// or export {};

To use it, import the module where you need to reference these typedefs:

const typedefs = require("./typedefs");
/** @type {typedefs.foo} */
const fb = { bar: "hello" };

You may wish to annotate typedefs.js as a @module or @namespace. Because I’m using “tsd-jsdoc” to generate a types.d.ts file, and due to the way TypeScript now interprets modules vs. namespaces, I’ve annotated my typedefs.js file as a @namespace and documented each typedef as a member of that namespace:

/**
 * @namespace typedefs
 */

/**
 * @typedef foo
 * @property {string} bar
 * @memberof typedefs
 */

Hope that helps.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)