Named exports vs default exports for types
What you'll learn: How to decide between named and default exports when sharing types across modules in TypeScript.
When you export types from a module, you face the same choice you do with values: use named exports or default exports. Each approach has different trade-offs that affect clarity, refactoring, and consistency.
Named exports for types
Named exports require you to specify the exact name when importing:
// types.ts
export type User = {
id: number;
name: string;
};
export type Product = {
id: number;
price: number;
};
// main.ts
import { User, Product } from './types';
Benefits:
- Multiple types per file without ambiguity
- Explicit naming prevents accidental misuse
- Easier to find usage across your codebase
- Refactoring tools can rename consistently
Default exports for types
Default exports let you choose any name when importing:
// User.ts
type User = {
id: number;
name: string;
};
export default User;
// main.ts
import UserType from './User'; // You pick the name
import U from './User'; // This also works
Benefits:
- Convenient for single-type modules
- Slightly less typing when importing
- Common pattern in some frameworks
The clarity trade-off
Named exports force consistency. Everyone uses User, not UserType, U, or Person. Default exports give flexibility, but that flexibility can hurt:
- Different files might import the same type with different names
- Searching for type usage becomes harder
- Merge conflicts increase when multiple people import differently
When to choose which
Prefer named exports for most TypeScript projects:
- You often export multiple types from one file
- You want consistent naming across your codebase
- You value refactoring safety and searchability
Consider default exports when:
- You have exactly one major type per file (rare)
- You're following a framework convention that requires it
- The file name clearly indicates the type (like
User.ts→User)
Key takeaway: Named exports provide better clarity and consistency for types in most TypeScript codebases. Default exports offer convenience but at the cost of potential naming confusion and harder refactoring.