Where can I store JSDoc typedef information to share across projects?

Viewed 1712

I'm adding @typedef JSDoc comments to the top of Javascript files to help define types (really, so that I can take advantage of some of the typescript benefits without learning it all today).

Where can I store JSDoc typedef information to share across projects in VSCode?

For example, can I store this in some external file and then use it in multiple .js files?

/**
 * @typedef {Object} SomeType
 * @property {String} id
 */
1 Answers

2021 Update

Edit tsconfig.json so that includes is relative to the root of your project and includes your type files as well. For example:

{
    "compilerOptions": {
      // ...
    },
    "include": ["./my-types.js", "src/**/*.js"],
}

If you're not nesting any of your projects under a folder like src/, then this would work on its own:

{
    "compilerOptions": {
      // ...
    },
    "include": ["**/*.js"],
}

Original Answer

You can put that code snippet in a .js file and then either add it to the files or include in each tsconfig.json file or use a reference directive:

///<reference path="path/to/shared-file.js" />

(These are the same two options you have for TypeScript declaration files.) It gets a little more complicated if your shared file is an ES6 module (with top-level ES6 imports or exports); then you'll need to either import it or use import types.

Related