Declaration Merging Basics
What you'll learn: How TypeScript automatically combines multiple interface declarations with the same name into one unified interface.
The Core Idea
Unlike type aliases (which would cause an error), interfaces have a special superpower: declaration merging. When you declare the same interface multiple times in the same scope, TypeScript doesn't complain—instead, it automatically merges all those declarations into a single interface definition.
Think of it like a team adding entries to a shared notebook. Each person can add their own properties on different pages, and TypeScript combines them all into one complete reference.
How It Works
interface User {
name: string;
}
interface User {
age: number;
}
// TypeScript merges these into:
// interface User {
// name: string;
// age: number;
// }
const person: User = {
name: "Alice",
age: 30
}; // ✓ Both properties required
Both name and age are now required properties of User, even though they were declared separately.
Why This Matters
Declaration merging is particularly useful when:
- Extending third-party library types without modifying their code
- Splitting large interface definitions across multiple files
- Gradually adding properties in different parts of your codebase
Unlike extending with extends, merging happens implicitly—no new interface name needed.
A Key Difference
This only works with interfaces. If you tried this with type aliases, TypeScript would throw a duplicate identifier error immediately.
Key Takeaway: TypeScript automatically merges multiple interface declarations with the same name in the same scope, combining all their properties into a single interface—a unique feature interfaces have over type aliases.