Declaration merging for namespaces
📖 Scenario: Imagine you are organizing a library system. You want to group related information about books and authors using namespaces. Sometimes, you need to add more details to the same namespace without rewriting it.
🎯 Goal: You will create two separate namespace declarations with the same name and see how TypeScript merges them into one. This helps keep your code organized and extendable.
📋 What You'll Learn
Create a namespace called
Library with a function getBookTitle that returns a string.Add a second
Library namespace with a function getAuthorName that returns a string.Call both functions and print their results to see the merged namespace in action.
💡 Why This Matters
🌍 Real World
In large projects, you often want to split code into parts but keep related things grouped. Declaration merging lets you add to the same namespace from different files or sections.
💼 Career
Understanding declaration merging helps you maintain and extend TypeScript codebases, especially when working with libraries or modular code.
Progress0 / 4 steps