This is a TypeScript-flavored JSDoc specific answer, but I'm having success using a triple-slash directive to "import" all the types from another file. This has the advantage of not actually adding an unused import
which can upset linters and bundlers.
I'm putting my shared types in one file called typedefs.js
like this:
// typedefs.js
/**
* @typedef {Object} Foo
* @property {string} bar
*/
/**
* @typedef {Object} Baz
* @property {number} buzz
*/
and then using /// <reference path="typedefs.js" />
in the other files to access the shared types like this:
// randomThing.js
/// <reference path="typedefs.js" />
/**
* Turn a Foo into a Baz
*
* @param {Foo} a
* @return {Baz}
export function (a) {
return { buzz: a.bar.length };
}
The tricky thing though is that now typedefs.js
is just being referenced in a comment, bundlers like rollup miss it completely. So I'm combining it with my old consts.js
that exports a few constants and is imported in at least one place. That way the typedefs are still included in the rollup output.
I hope someone else finds this helpful.
p.s. rollup will completely exclude a pure JSDoc typedefs.js
file _even if you have import './typedefs.js'
because of tree-shaking! Gotta run rollup with --no-treeshake
to keep those comments in the rollup output.
@global
to the definitions, or experiment with the different ways to namespace stuff in JSDoc (confusing, IMHO, and for my own purposes which only is WebStorm inlineinfo/help and HTML API documentation works well).