Merging Properties in Interfaces
What you'll learn: How TypeScript combines properties when you declare the same interface multiple times.
How Property Merging Works
When you declare an interface with the same name twice (or more), TypeScript doesn't replace the old definition—it merges them together. All properties from every declaration become part of one unified interface.
interface User {
name: string;
}
interface User {
age: number;
}
// TypeScript merges both declarations into:
// interface User {
// name: string;
// age: number;
// }
const person: User = {
name: "Alice",
age: 30
}; // ✓ Both properties required
The Merging Rules
Same property, same type = allowed:
interface Config {
debug: boolean;
}
interface Config {
debug: boolean; // ✓ Same type, no conflict
}
Same property, different type = error:
interface Settings {
port: number;
}
interface Settings {
port: string; // ✗ Error! Conflicting types
}
Think of it like stacking transparent sheets—each new declaration adds more detail to the same picture, but if two sheets show conflicting colors in the same spot, you get an error.
Practical Example
interface Product {
id: string;
name: string;
}
interface Product {
price: number;
inStock: boolean;
}
const item: Product = {
id: "abc123",
name: "Widget",
price: 29.99,
inStock: true
}; // All four properties required
Key Takeaway: TypeScript merges interface declarations with the same name by combining all their properties, but throws an error if the same property is declared with incompatible types across declarations.