Q43: What is Declaration Merging?

Difficulty:MediumTopic:Declaration Merging

Declaration Merging occurs when the TypeScript compiler automatically combines multiple declarations sharing the same name into a single unified type definition.

Supported for: interface definitions, namespace definitions, and merging namespaces into functions or classes.

Interface Declaration Merging
interface User {
  name: string;
}

interface User {
  age: number;
}

const user: User = { name: "Alice", age: 30 }; // Merged into unified interface!
💡 TypeScript merges multiple interface definitions sharing the same identifier in the same scope.
Key Takeaways
  • Interfaces automatically merge properties across identical namespace names.