Importing Types from Other Files
What You'll Learn
You'll learn how to reference JSDoc type definitions created in other files, allowing you to share and reuse types across your entire project without duplication.
Why Import Types?
When you've defined custom types using @typedef in one file, you don't want to copy-paste those definitions everywhere. Just like importing functions or constants, you can import type definitions to maintain a single source of truth.
Think of it like a shared dictionary: instead of every team member keeping their own copy (which might drift out of sync), everyone references the same master dictionary when they need to look something up.
How to Import Types
Use the @typedef tag with an import() syntax to bring in types from other modules:
// types.js - Your type definitions file
/**
* @typedef {Object} User
* @property {string} id
* @property {string} name
* @property {string} email
*/
// app.js - Consuming the type
/**
* @typedef {import('./types.js').User} User
*/
/**
* @param {User} user
* @returns {string}
*/
function getUserGreeting(user) {
return `Hello, ${user.name}!`;
}
The import('./types.js').User syntax tells JSDoc to look in types.js and grab the User type definition. You then re-export it locally with the outer @typedef, making it available throughout your current file.
Keeping It DRY
This pattern lets you create a centralized types.js file where all your custom types live, then import only what each module needs. Changes to type definitions automatically propagate everywhere they're used—no manual updates required.
Key Takeaway: Use @typedef {import('./path').TypeName} TypeName to reference types defined in other files, creating reusable, maintainable type definitions across your project.