Namespace merging lets you combine multiple pieces of code with the same name into one. This helps organize code and add features without changing the original part.
Namespace merging in Typescript
namespace MyNamespace {
export function greet() {
console.log('Hello');
}
}
namespace MyNamespace {
export function bye() {
console.log('Goodbye');
}
}Each namespace block with the same name merges into one combined namespace.
Use export to make functions or variables accessible outside the namespace.
Animals merge to hold both dog and cat functions.namespace Animals {
export function dog() {
console.log('Woof');
}
}
namespace Animals {
export function cat() {
console.log('Meow');
}
}hammer and screwdriver are combined under one Tools namespace.namespace Tools {
export const hammer = 'Hammer';
}
namespace Tools {
export const screwdriver = 'Screwdriver';
}This program shows two namespace blocks named Greetings. They merge so you can call both hello and goodbye functions from the same namespace.
namespace Greetings {
export function hello() {
console.log('Hello!');
}
}
namespace Greetings {
export function goodbye() {
console.log('Goodbye!');
}
}
Greetings.hello();
Greetings.goodbye();Namespace merging only works with namespaces, not with modules or classes.
All merged parts must be in the same scope (like the same file or global scope) to merge correctly.
Use namespace merging carefully to avoid confusing code structure.
Namespace merging combines multiple namespace blocks with the same name into one.
It helps organize and extend code without changing original parts.
Use export to share functions or variables outside the namespace.
Practice
Solution
Step 1: Understand namespace merging concept
TypeScript allows multiple namespace declarations with the same name to combine their members into one.Step 2: Effect of merging
All functions, variables, or types inside these namespaces become part of a single namespace accessible together.Final Answer:
They merge into a single namespace combining all members. -> Option BQuick Check:
Namespace merging = combined members [OK]
- Thinking second namespace replaces the first
- Expecting a compile error for duplicate namespaces
- Assuming namespaces remain separate
Solution
Step 1: Identify export keyword usage
To make a function accessible outside the namespace, it must be marked withexport.Step 2: Check syntax correctness
Only namespace MySpace { export function greet() { return 'Hi'; } } correctly usesexport function greet()inside the namespace.Final Answer:
namespace MySpace { export function greet() { return 'Hi'; } } -> Option AQuick Check:
Export keyword needed for external access [OK]
- Omitting 'export' keyword
- Using 'public' keyword which is invalid in namespaces
- Incorrect export syntax without 'function' keyword
namespace Alpha {
export const value = 10;
}
namespace Alpha {
export function getValue() { return value * 2; }
}
console.log(Alpha.getValue());What is the output when this code runs?
Solution
Step 1: Understand namespace merging effect
Both namespace blocks named 'Alpha' merge, sovalueandgetValueare in the same namespace.Step 2: Evaluate function output
getValue()returnsvalue * 2, which is 10 * 2 = 20.Final Answer:
20 -> Option AQuick Check:
Namespace members combined, output = 20 [OK]
- Thinking namespaces are separate causing undefined
- Expecting runtime error due to multiple declarations
- Confusing output with original value
namespace Beta {
export let count = 5;
}
namespace Beta {
let count = 10;
export function showCount() { return count; }
}
console.log(Beta.showCount());Solution
Step 1: Analyze variable declarations in merged namespace
The second namespace declares a localcountwithout export, which shadows the exportedcountfrom the first namespace.Step 2: Effect on function output
showCount()returns the innercount(10), not the exported one (5), which may be unexpected.Final Answer:
The inner 'count' shadows the exported 'count', causing unexpected output. -> Option DQuick Check:
Local variable shadows exported one [OK]
- Assuming redeclaration causes error
- Expecting 'showCount' to return 5
- Thinking export is needed on inner variable
Utils defined in another file by adding a new exported function double that doubles a number. Which is the correct way to do this using namespace merging?Solution
Step 1: Use 'namespace' keyword to extend existing namespace
To add members to an existing namespace, declare a new namespace block with the same name.Step 2: Export the new function properly
The function must be exported to be accessible outside the namespace.Final Answer:
namespace Utils { export function double(x: number) { return x * 2; } } -> Option CQuick Check:
Extend with 'namespace' and export function [OK]
- Using 'module' instead of 'namespace'
- Not exporting the new function
- Declaring function without namespace
Ready to prove it?
Pass the test and this pattern moves unknown
