Sub topic 9
Sharing Types Across Files
This chapter assumes you already know how to write @typedef and @callback definitions, that you understand ES module import and export, and that you have type checking turned on with // @ts-check or checkJs. Once a project has more than a few files, you will want to define a type such as User in one place and use it everywhere. JSDoc has no native import tag of its own. TypeScript fills that gap with import() type expressions, which TypeScript-aware editors and tsc understand.
How a typedef becomes importable
TypeScript treats a file as a module if it contains at least one top-level import or export. Inside a module, every @typedef and @callback is exported automatically. You do not need an export keyword for them, and you cannot add one.
// src/user.js
/**
* @typedef {Object} User
* @property {string} id - Unique identifier.
* @property {string} name - Display name.
* @property {string} [email] - Optional email address.
*/
/**
* Creates a user without an email address.
* @param {string} id
* @param {string} name
* @returns {User}
*/
export function createUser(id, name) {
return { id, name };
}If a file holds only type definitions and no runtime code, add export {}; at the bottom. Without that line, the file counts as a global script. Its typedefs then leak into every file as globals, and import() cannot find them as exports.
The import() type expression
To use User in another file, give an import() expression the module path and then name the type you want:
// src/describe.js
/** @typedef {import('./user.js').User} User */
/**
* @param {User} user
* @returns {string}
*/
export function describe(user) {
return `${user.name} (${user.id})`;
}The @typedef line creates a local alias, so the rest of the file can just write User. You can also write the import inline, as in @param {import('./user.js').User} user. That works fine for a single use, but it gets noisy when the type appears several times.
The import() expression exists only in the type system. It adds no runtime import, does not load user.js, and cannot create a circular dependency at runtime.
The @import tag (TypeScript 5.5+)
TypeScript 5.5 added an @import tag that reads like an ES import statement. It can bring in several types at once:
// src/report.js
/** @import { User } from './user.js' */
/**
* @param {User[]} users
* @returns {string[]}
*/
export function names(users) {
return users.map((u) => u.name);
}Use @import if every tool in your project supports TypeScript 5.5 or later. Use the import() alias if you need to work with older tooling.
Referring to values, not just types
import() can also point at a runtime export. Combine it with typeof to get the type of a function or class:
// src/factory.js
/** @typedef {typeof import('./user.js').createUser} CreateUser */
/**
* @param {CreateUser} make
* @returns {import('./user.js').User}
*/
export function makeAdmin(make) {
return make('0', 'admin');
}Common errors
| Symptom | Cause | Fix |
|---|---|---|
| `Cannot find module './user'` | The extension is missing under Node16 or NodeNext resolution | Write `'./user.js'` |
| `Module has no exported member 'User'` | The source file has no `import` or `export`, so it is a script | Add `export {};` |
| The type silently becomes `any` | Checking is off for this file | Add `// @ts-check` or enable `checkJs` |
| Path works in one file but fails in another | The path is relative to the importing file, not the project root | Adjust `./` or `../` |
Exercise
You move the User typedef into a new file src/types.js that contains only JSDoc comments. In describe.js, /** @typedef {import('./types.js').User} User */ now reports "Module has no exported member 'User'". Why, and what single line fixes it?
Show answer
A file with no top-level import or export is treated as a global script, so its typedefs are not module exports. Adding export {}; to the end of src/types.js makes it a module, and User becomes importable.
Exercise
Does /** @typedef {import('./user.js').User} User */ cause user.js to be loaded when describe.js runs?
Show answer
No. The import() type expression exists only for the type checker. It produces no runtime import, so user.js is not loaded or executed because of it.