What is declaration merging in TypeScript namespaces?
Declaration merging is when multiple namespace declarations with the same name combine their contents into a single namespace.
Click to reveal answer
beginner
How does TypeScript handle multiple namespace declarations with the same name?
TypeScript merges all declarations with the same namespace name into one, combining their members like variables, functions, or interfaces.
Click to reveal answer
intermediate
Can you add functions and variables in separate namespace declarations and have them accessible together?
Yes, functions and variables declared in separate namespace blocks with the same name are merged and accessible as if declared in one namespace.
Click to reveal answer
advanced
What happens if two merged namespaces declare the same function name with different implementations?
TypeScript will merge the declarations, but the last implementation will overwrite previous ones, so only one function body is used.
Click to reveal answer
intermediate
Why is declaration merging useful in namespaces?
It allows splitting code into multiple parts or files while keeping related code grouped under one namespace, improving organization and modularity.
Click to reveal answer
What does declaration merging for namespaces allow you to do?
AMerge classes with namespaces
BCombine multiple namespace declarations with the same name into one
CAutomatically convert namespaces to classes
DPrevent namespaces from having the same name
✗ Incorrect
Declaration merging combines multiple namespace declarations with the same name into a single namespace.
If two namespaces with the same name declare different variables, what happens?
AAn error occurs due to duplicate declarations
BOnly the first namespace's variables are kept
CVariables from both namespaces are merged and accessible
DVariables are renamed automatically
✗ Incorrect
Variables from both namespaces are merged and accessible as if declared in one namespace.
Can declaration merging be used across multiple files?
AYes, namespaces with the same name in different files merge
BOnly if files are imported as modules
CNo, it only works within a single file
DOnly for classes, not namespaces
✗ Incorrect
Namespaces with the same name declared in different files merge into one namespace.
What happens if two merged namespaces declare a function with the same name but different code?
AThe functions are merged into one combined function
BTypeScript throws an error
CBoth functions exist separately
DThe last declared function overwrites the previous one
✗ Incorrect
The last declared function overwrites the previous one in merged namespaces.
Why might you use declaration merging for namespaces?
ATo split related code into multiple parts but keep it under one namespace
BTo avoid using namespaces altogether
CTo automatically generate classes
DTo merge unrelated code into one file
✗ Incorrect
Declaration merging helps organize related code by splitting it into parts while keeping it under one namespace.
Explain how declaration merging works for namespaces in TypeScript and why it is useful.
Think about how you can write parts of a namespace in different places and still access all members together.
You got /4 concepts.
Describe what happens when two merged namespaces declare a function with the same name but different implementations.
Consider which function code TypeScript uses when names collide.
You got /4 concepts.
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: