📖 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
1
Create the first Library namespace
Create a namespace called Library with a function getBookTitle that returns the string "The Great Gatsby".
Typescript
Hint
Use namespace Library { export function getBookTitle() { return "The Great Gatsby"; } }
2
Add a second Library namespace
Add another namespace called Library with a function getAuthorName that returns the string "F. Scott Fitzgerald".
Typescript
Hint
Write a second namespace Library block with the new function inside.
3
Call both functions from the merged Library namespace
Write code to call Library.getBookTitle() and Library.getAuthorName(), storing their results in variables title and author.
Typescript
Hint
Use const title = Library.getBookTitle(); and const author = Library.getAuthorName();
4
Print the results
Print the variables title and author using two separate console.log statements.
Typescript
Hint
Use console.log(title); and console.log(author); to show the results.
Practice
(1/5)
1. What does declaration merging for namespaces in TypeScript allow you to do?
easy
A. Merge classes and interfaces automatically
B. Combine multiple namespace blocks with the same name into one namespace
C. Create multiple namespaces with different names in one file
D. Prevent namespaces from being extended
Solution
Step 1: Understand declaration merging concept
Declaration merging means combining multiple declarations of the same name into one.
Step 2: Apply to namespaces
For namespaces, this means multiple blocks with the same namespace name merge into a single namespace.
Final Answer:
Combine multiple namespace blocks with the same name into one namespace -> Option B
Quick Check:
Declaration merging = Combine namespaces [OK]
Hint: Namespaces with same name merge into one combined namespace [OK]
Common Mistakes:
Thinking namespaces with different names merge
Confusing merging with class or interface merging
Believing merging prevents extension
2. Which of the following is the correct syntax to merge two namespace declarations named Utils?
easy
A. namespace Utils { export function a() {} } namespace Utils { export function b() {} }
B. namespace Utils { export function a() {} } namespace Utils2 { export function b() {} }
C. namespace Utils { export function a() {} } class Utils { }
D. namespace Utils { export function a() {} } interface Utils { }
Solution
Step 1: Check namespace names
Only namespaces with the exact same name merge. namespace Utils { export function a() {} } namespace Utils { export function b() {} } uses "Utils" twice.
Step 2: Verify syntax correctness
namespace Utils { export function a() {} } namespace Utils { export function b() {} } correctly declares two namespace blocks with functions inside, which will merge.
Final Answer:
namespace Utils { export function a() {} } namespace Utils { export function b() {} } -> Option A
Quick Check:
Same namespace name + multiple blocks = merge [OK]
Hint: Use identical namespace names to merge declarations [OK]
Common Mistakes:
Using different namespace names expecting merge
Mixing namespace with class or interface of same name
Incorrect syntax for namespace blocks
3. What will be the output of the following TypeScript code?
namespace Data {
export const value = 10;
}
namespace Data {
export function getValue() { return value; }
}
console.log(Data.getValue());
medium
A. 10
B. undefined
C. ReferenceError
D. SyntaxError
Solution
Step 1: Understand namespace merging
Both namespace blocks named "Data" merge into one namespace containing both value and getValue.
Step 2: Evaluate function call
getValue() returns value which is 10, so console.log prints 10.
Final Answer:
10 -> Option A
Quick Check:
Merged namespace members accessible = 10 [OK]
Hint: Merged namespaces share members, so functions access variables [OK]
Common Mistakes:
Assuming separate namespaces, so value is undefined
Expecting runtime errors due to merging
Confusing variable scope inside merged namespaces
4. Identify the error in the following code that attempts to merge namespaces: