Exporting Inline Type Definitions
You'll learn: How to combine type definitions and exports into a single, concise statement using the export keyword prefix.
What This Means
So far, you've likely written your types in one statement, then exported them in another—or defined them first and exported later. But TypeScript lets you do both at once by simply placing export directly before your type definition.
Think of it like signing a letter and addressing the envelope in one motion instead of two separate steps.
The Inline Export Pattern
Instead of this two-step approach:
type User = {
id: number;
name: string;
};
export { User };
You can export the type as you define it:
export type User = {
id: number;
name: string;
};
This works with any type definition: type aliases, interfaces, and even inline object types in function signatures (though that's less common).
For interfaces:
export interface Product {
sku: string;
price: number;
}
Why Use Inline Exports?
Clarity: The export keyword at the start immediately signals "this type is public API." Anyone reading your code knows instantly that this type is meant for consumers.
Brevity: One line instead of two. Less ceremony, especially in modules with many types.
Convention: It's the most common pattern in TypeScript codebases—most libraries export types this way.
When Both Are Defined and Exported Together
You can export multiple types inline in the same file:
export type UserId = number;
export interface UserProfile {
userId: UserId;
email: string;
}
Each gets its own export prefix. This keeps your module's public API clear and organized.
Key Takeaway: Prefix type definitions with export to define and export in one statement—the standard, concise way to share types across modules.