Advertisement
Intermediate TypeScript, Namespaces & Declaration Merging

What is declaration merging, and how does TypeScript combine multiple 'interface' declarations with the same name?

Short answer

When you declare an interface with the same name multiple times (even in different files), TypeScript automatically MERGES all their members into a single combined interface, rather than treating the second declaration as an error or an override — this is unique to interfaces…

Save TypeScript mock interview
Advertisement
AModel answer
When you declare an interface with the same name multiple times (even in different files), TypeScript automatically MERGES all their members into a single combined interface, rather than treating the second declaration as an error or an override — this is unique to interfaces (type aliases do NOT support this).
interface Window {
  myCustomProp: string;
}
interface Window {
  anotherProp: number;
}
// merged result: Window now has both myCustomProp AND anotherProp
Real-world example
Extending a built-in global interface (like Window or Express's Request) with additional properties from a separate file.
Likely follow-ups
  • Why does this same automatic merging NOT work with 'type' aliases?
Related topics