Namespace merging in Typescript - Time & Space Complexity
When we use namespace merging in TypeScript, we combine multiple pieces of code under one name.
We want to see how the time to process this merging grows as the code size grows.
Analyze the time complexity of the following code snippet.
namespace Animals {
export function speak() { console.log("Animal speaks"); }
}
namespace Animals {
export function run() { console.log("Animal runs"); }
}
Animals.speak();
Animals.run();
This code merges two namespaces named Animals and calls their functions.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The compiler processes each namespace block to merge their contents.
- How many times: Once for each namespace declaration with the same name.
As the number of namespace blocks with the same name grows, the compiler merges more pieces together.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 merge steps |
| 100 | About 100 merge steps |
| 1000 | About 1000 merge steps |
Pattern observation: The work grows directly with the number of namespace parts to merge.
Time Complexity: O(n)
This means the time to merge namespaces grows in a straight line as you add more parts.
[X] Wrong: "Merging namespaces happens instantly no matter how many parts there are."
[OK] Correct: Each namespace part must be processed, so more parts mean more work and more time.
Understanding how merging works helps you explain how your code scales and how the compiler handles your code structure.
"What if we nested namespaces inside each other instead of merging at the same level? How would the time complexity change?"
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
