Re-exporting Types from Other Modules
What you'll learn: How to create "barrel files" that consolidate and re-export types from multiple modules using the export { Type } from './module' syntax.
The Central Hub Pattern
Re-exporting lets you create index files (called "barrel files") that act as central distribution points for types. Instead of importing from many scattered files, consumers import from one convenient location.
Think of it like a warehouse distribution center: instead of customers going to dozens of different factories, they visit one warehouse that gathers products from all the factories and makes them available in one place.
Basic Re-export Syntax
The key pattern is export { Type } from './module':
// user.types.ts
export type User = {
id: number;
name: string;
};
// product.types.ts
export type Product = {
id: number;
title: string;
};
// index.ts (barrel file)
export { User } from './user.types';
export { Product } from './product.types';
Now consumers import from the barrel:
import { User, Product } from './types';
// Instead of:
// import { User } from './types/user.types';
// import { Product } from './types/product.types';
Aggregating Multiple Types
You can re-export multiple types from the same module:
export { User, UserRole, UserPermissions } from './user.types';
export { Product, ProductCategory } from './product.types';
This creates a clean public API where all your domain types are available from one entry point, while keeping the internal organization modular.
Key Takeaway: Barrel files using export { Type } from './module' centralize type exports, simplifying imports and creating cleaner public APIs for your modules.