Set your Publisher ID in src/config/ads.ts to display live ads.
Interfaces vs Type Aliases
Choosing between declaration merging and flexible union/intersection types
Both interface and type allow you to define object structures. Understanding their subtle differences—such as declaration merging and union capabilities—is essential for library authors and application developers.
Comparing Interfaces and Type Aliases
interface: Can be extended using extends. Supports declaration merging (defining the same interface name multiple times merges their fields). Ideal for public APIs and libraries.
type: Can represent primitives, unions (type Status = "open" | "closed"), intersections, and tuples. Cannot be reopened for declaration merging.
Rule of thumb: Default to interface for object shapes until you need union types or complex conditional type mappings.
// Interface extension
interface Identifiable {
id: string;
createdAt: Date;
}
interface UserProfile extends Identifiable {
username: string;
email: string;
}
// Type alias with union & intersection
type Role = 'Admin' | 'Editor' | 'Subscriber';
type SystemUser = UserProfile & { role: Role };
const adminUser: SystemUser = {
id: 'usr_8492',
createdAt: new Date(),
username: 'SarahDev',
email: 'sarah@codexia.edu',
role: 'Admin'
};
console.log(`User: ${adminUser.username} (${adminUser.role})`);Set your Publisher ID in src/config/ads.ts to display live ads.
Test Yourself With Exercise
Which capability is supported by interfaces but NOT by type aliases?