TILJSDocTypeScript

Import Types from other modules in JSDoc


Today I learned about a less verbose way of importing types from a different module when working in a JSDoc codebase.

Instead of using @typedef to locally redeclare a type, you can straight up use @import:

// instead of this:
/** @typedef {import("./other-file.js").MyType} MyType */

// do this instead:
/** @import { MyType } from "other-file.js" */

You can then use the MyType type in this module.

I find this syntax much nicer to work with! I also like that the syntax is basically the same as in ESM (except for the @ prefix).

Disclaimer

Even though this is technically not part of JSDoc, it works in VSCode (and probably any other editor that uses TypeScript for type checking).

Source

Source and more extensive example: https://devblogs.microsoft.com/typescript/announcing-typescript-5-5-beta/#type-imports-in-jsdoc