Declaration Merging
Declaration Merging (π solution)
π¨βπΌ Declaration merging is complete! The
Config interface was declared in the
global scope in index.ts with appName, then augmented in config-augment.ts
with theme and maxConnections using declare global. When you import both
files, TypeScript merges all the global Config declarations together!This is why
config can have properties from both filesβTypeScript sees all
the declare global { interface Config { ... } } blocks and combines them into
a single merged interface.π¦ This feature is especially useful for:
- Extending library types - Add properties to third-party interfaces
- Plugin systems - Each plugin can augment a shared config interface in its own file
- Global augmentation - Add properties to
Windowor other globals - Modular configuration - Split interface definitions across multiple files
This is the one capability that interfaces have that type aliases don't!
Declaration merging is powerful but dangerous for everyday types. Any code
anywhere can secretly add properties to your interfaceβmaking it hard to know
what a type actually contains without searching your entire codebase.
This is why many teams prefer type aliases for application code (they can't
be merged) and reserve interfaces for library APIs that genuinely need
extension.


