export type for Type-Only Exports
What you'll learn: How to use export type to explicitly mark exports as type-only, improving build optimization and clarifying your module's intent.
What Problem Does export type Solve?
Just as import type tells bundlers "this import is only for type-checking," export type signals "this export is only a type, never a runtime value." This explicit declaration helps tools optimize your compiled JavaScript and makes your intentions crystal clear.
Think of it like labeling boxes in a warehouse: a box marked "blueprints only" tells workers they don't need to load it onto the delivery truck—it's just for planning. Similarly, export type tells bundlers and compilers they can safely strip this export from the final JavaScript.
Basic Syntax
// Exporting a type-only definition
export type UserId = string;
export type User = {
id: UserId;
name: string;
};
// Exporting an interface as type-only
export type { UserConfig } from './config';
When to Use export type
Use export type when you want to:
- Signal intent: Make it obvious that something is purely for types
- Optimize builds: Help bundlers eliminate unused type code
- Prevent confusion: Avoid ambiguity between type and value exports
// Clear: this module only exports types
export type Status = 'pending' | 'active' | 'inactive';
export type StatusMap = Record<Status, string>;
// Versus mixed exports that might confuse readers
export const DEFAULT_STATUS = 'pending';
export type Status = 'pending' | 'active' | 'inactive';
Re-exporting Types
You can re-export types from other modules using export type:
// Re-export specific types
export type { User, UserId } from './user-types';
// This prevents accidentally exporting values
// export type { userSchema } from './user-types'; // Error if userSchema is a value!
Key Takeaway: Use export type to explicitly mark type-only exports, making your code's intent clear to developers and enabling build tools to optimize away type-only code more reliably.