Declaration merging lets you combine multiple pieces of code with the same name into one. For namespaces, this means you can add more things to a namespace in different places.
Declaration merging for namespaces in Typescript
namespace MyNamespace {
// first part
}
namespace MyNamespace {
// second part
}Both namespace blocks must have the same name to merge.
All merged parts combine into one namespace when the program runs.
Animals. Both functions become part of one namespace.namespace Animals {
export function dog() {
console.log('Woof!');
}
}
namespace Animals {
export function cat() {
console.log('Meow!');
}
}Tools namespace from separate declarations.namespace Tools {
export const hammer = 'Hammer';
}
namespace Tools {
export const screwdriver = 'Screwdriver';
}This program shows how two namespace declarations with the same name merge. Both hello and goodbye functions are available under Greetings.
namespace Greetings {
export function hello() {
console.log('Hello!');
}
}
namespace Greetings {
export function goodbye() {
console.log('Goodbye!');
}
}
Greetings.hello();
Greetings.goodbye();Declaration merging only works if the namespace names are exactly the same.
Use export inside namespaces to make functions or variables accessible outside.
Declaration merging helps keep code organized and easy to extend.
Declaration merging combines multiple namespace blocks with the same name into one.
This allows adding new members to a namespace in different places.
It helps organize and extend code without changing original namespace code.
Practice
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 BQuick Check:
Declaration merging = Combine namespaces [OK]
- Thinking namespaces with different names merge
- Confusing merging with class or interface merging
- Believing merging prevents extension
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 AQuick Check:
Same namespace name + multiple blocks = merge [OK]
- Using different namespace names expecting merge
- Mixing namespace with class or interface of same name
- Incorrect syntax for namespace blocks
namespace Data {
export const value = 10;
}
namespace Data {
export function getValue() { return value; }
}
console.log(Data.getValue());Solution
Step 1: Understand namespace merging
Both namespace blocks named "Data" merge into one namespace containing bothvalueandgetValue.Step 2: Evaluate function call
getValue()returnsvaluewhich is 10, soconsole.logprints 10.Final Answer:
10 -> Option AQuick Check:
Merged namespace members accessible = 10 [OK]
- Assuming separate namespaces, so value is undefined
- Expecting runtime errors due to merging
- Confusing variable scope inside merged namespaces
namespace Tools {
export function toolA() { return 'A'; }
}
namespace Tools {
const secret = 'hidden';
export function toolB() { return secret; }
}Solution
Step 1: Check variable scope in merged namespaces
The variablesecretis declared but not exported, so it is private to its namespace block.Step 2: Analyze usage of 'secret' in toolB
FunctiontoolBtries to returnsecret, but sincesecretis not accessible outside its block, this causes an error.Final Answer:
Cannot access non-exported 'secret' in merged namespace -> Option CQuick Check:
Non-exported members are block-scoped [OK]
- Assuming all members merge regardless of export
- Thinking duplicate namespace names cause syntax errors
- Believing functions can't be declared in merged namespaces
console.log(Shapes.area(5)); output?namespace Shapes {
export function area(radius: number) {
return Math.PI * radius * radius;
}
}
namespace Shapes {
export function perimeter(radius: number) {
return 2 * Math.PI * radius;
}
export function area(radius: number) {
return Math.round(Math.PI * radius * radius);
}
}
console.log(Shapes.area(5));Solution
Step 1: Understand merging with duplicate function names
When namespaces merge, later declarations override earlier ones for the same member name.Step 2: Identify which 'area' function is used
The second namespace redefinesareawith rounding. SoShapes.area(5)calls the rounded version.Step 3: Calculate output
Math.PI * 5 * 5 ≈ 78.5398, rounded is 79.Final Answer:
79 -> Option DQuick Check:
Later declarations override earlier ones [OK]
- Expecting both functions to coexist without override
- Thinking duplicate function names cause errors
- Ignoring rounding in second area function
Ready to prove it?
Pass the test and this pattern moves unknown
