Course contentsShow
← TypeScript
Lesson 371 of 1,7968. Interfaces vs Type AliasesFree lesson

Declaration Merging: The Key Interface Superpower

Interfaces can be declared multiple times and automatically merge, enabling extensibility patterns impossible with type aliases.

Declaration Merging: The Key Interface Superpower

What You'll Learn

You'll discover why interfaces can do something type aliases can't: automatically combine multiple declarations of the same name into one unified type.

The Core Idea

Declaration merging is TypeScript's ability to take multiple interface declarations with the same name and merge them into a single interface containing all properties from each declaration. This is exclusive to interfaces—type aliases will throw an error if you try to declare them twice.

Think of it like a bulletin board where multiple people can add notes. Each person (each interface declaration) adds their information to the same board (interface name), and TypeScript automatically combines all the notes into one complete picture.

How It Works

interface User {
  name: string;
}

interface User {
  email: string;
}

// TypeScript automatically merges these into:
// interface User {
//   name: string;
//   email: string;
// }

const user: User = {
  name: "Alice",
  email: "alice@example.com"
};

This works seamlessly across the same file or even across different files in your project. The merged interface includes all properties from all declarations.

Why This Matters

Declaration merging enables extensibility patterns impossible with type aliases. You can augment existing interfaces (including third-party library types) without modifying the original source. This is foundational for advanced techniques like augmenting global types or extending library definitions.

Type aliases, by contrast, are single declarations only:

type Product = { id: number };
// type Product = { name: string }; // ❌ Error: Duplicate identifier

Key Takeaway: Interfaces support declaration merging, allowing multiple declarations to combine automatically—a superpower type aliases don't have, making interfaces the choice for extensible APIs and library typings.