Objects & Interfaces
Declaration Merging
Master interface declaration merging in TypeScript, augmenting third-party library types, namespace merging, and global scope expansion.
Declaration merging is widely used to augment global browser interfaces (like Window or ProcessEnv) and extend third-party library types without modifying source files in node_modules.
1. How Interface Declaration Merging Works
When two interfaces share the exact same identifier in the same namespace, TypeScript merges their properties:
2. Real-World Use Case: Augmenting Global Window and ProcessEnv
In web and Node.js applications, you frequently attach custom global variables (e.g. Google Analytics gtag, or custom environment variables). Use interface declaration merging to type them safely:
Augmenting global Window
Augmenting Node.js process.env
3. Merging Method Signatures
When merging interfaces containing methods, the merged method signatures act as Function Overloads. Later declarations take precedence over earlier declarations.
4. Interactive Code Playground
Test declaration merging below:
5. Common Pitfalls & Edge Cases
6. Summary Table
| Merge Type | Supported? | Description |
|---|---|---|
| Interface + Interface | Yes | Merges properties into single interface |
| Namespace + Namespace | Yes | Merges exported namespace members |
| Class + Interface | Yes | Adds interface properties to class shape |