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 Window or 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.

Please set the playground first

Loading "Declaration Merging"
Loading "Declaration Merging"
Login to get access to the exclusive discord channel.
Loading Discord Posts