A namespace helps you group related code together under one name. It keeps your code organized and avoids name clashes.
Namespace declaration in Typescript
Introduction
Syntax
Typescript
namespace NamespaceName {
// code here
}The namespace keyword starts the block.
All code inside belongs to that namespace.
Examples
Animals with a class Dog inside it.Typescript
namespace Animals {
export class Dog {
bark() {
console.log('Woof!');
}
}
}MathUtils contains a function add that adds two numbers.Typescript
namespace MathUtils {
export function add(a: number, b: number): number {
return a + b;
}
}Greetings has a constant and a function to greet someone.Typescript
namespace Greetings {
export const hello = 'Hello';
export function greet(name: string) {
console.log(`${hello}, ${name}!`);
}
}Sample Program
This program defines a namespace Vehicles with a class Car. Then it creates a car and calls its drive method.
Typescript
namespace Vehicles {
export class Car {
drive() {
console.log('Driving a car');
}
}
}
const myCar = new Vehicles.Car();
myCar.drive();Important Notes
Use export inside namespaces to make code accessible outside.
Namespaces are mostly used in older TypeScript code; modern projects often use modules instead.
Namespaces help avoid name conflicts by grouping code under a unique name.
Summary
Namespaces group related code under one name.
Use export to share code outside the namespace.
Namespaces help keep code organized and avoid name clashes.
Practice
1. What is the main purpose of a
namespace in TypeScript?easy
Solution
Step 1: Understand what namespaces do
Namespaces group related code together to keep it organized.Step 2: Recognize the benefit of namespaces
They help avoid name clashes by keeping code under one name.Final Answer:
To group related code under one name and avoid name clashes -> Option DQuick Check:
Namespaces = Group code + avoid clashes [OK]
Hint: Namespaces group code to avoid name conflicts [OK]
Common Mistakes:
- Thinking namespaces create new variable types
- Confusing namespaces with functions
- Assuming namespaces import libraries
2. Which of the following is the correct syntax to declare a namespace named
Utils in TypeScript?easy
Solution
Step 1: Recall the correct keyword for namespaces
TypeScript uses the keywordnamespaceto declare namespaces.Step 2: Check the syntax format
The correct syntax isnamespace Name { }, sonamespace Utils { }is correct.Final Answer:
namespace Utils { } -> Option AQuick Check:
Namespace syntax = namespace Name { } [OK]
Hint: Use 'namespace Name { }' to declare namespaces [OK]
Common Mistakes:
- Using 'module' instead of 'namespace'
- Adding '=' sign in declaration
- Using 'package' keyword which is invalid
3. What will be the output of the following TypeScript code?
namespace MathOps {
export function add(a: number, b: number) {
return a + b;
}
}
console.log(MathOps.add(3, 4));medium
Solution
Step 1: Understand the namespace and export usage
The functionaddis inside namespaceMathOpsand is exported, so it can be accessed outside.Step 2: Evaluate the function call
CallingMathOps.add(3, 4)returns 3 + 4 = 7.Final Answer:
7 -> Option BQuick Check:
Exported function call returns sum 7 [OK]
Hint: Exported namespace members are accessible outside [OK]
Common Mistakes:
- Forgetting to export the function
- Trying to call function without namespace prefix
- Expecting runtime errors due to namespace
4. Identify the error in this TypeScript namespace code:
namespace Data {
function getData() {
return "info";
}
}
console.log(Data.getData());medium
Solution
Step 1: Check function accessibility inside namespace
The functiongetDatais declared but not exported, so it's private to the namespace.Step 2: Understand why console.log(Data.getData()) fails
SincegetDatais not exported, it cannot be accessed outside the namespace, causing an error.Final Answer:
Function getData is not exported, so it cannot be accessed outside -> Option AQuick Check:
Export missing = access error [OK]
Hint: Export functions to access them outside namespaces [OK]
Common Mistakes:
- Assuming all namespace members are public by default
- Thinking namespace name is invalid
- Ignoring export keyword importance
5. Given two namespaces below, what is the correct way to call the
log function from Logger inside App?namespace Logger {
export function log(message: string) {
console.log("LOG: " + message);
}
}
namespace App {
export function run() {
// Call Logger.log here
}
}
App.run();hard
Solution
Step 1: Understand namespace access rules
Namespaces are accessed by their name followed by dot and member name, soLogger.logis correct.Step 2: Check the options for calling the function
OnlyLogger.log("App started")correctly calls the exported function from the Logger namespace.Final Answer:
Logger.log("App started"); -> Option CQuick Check:
Namespace function call = NamespaceName.member [OK]
Hint: Call namespace functions with NamespaceName.function() [OK]
Common Mistakes:
- Calling function without namespace prefix
- Using invalid syntax like '::'
- Assuming nested namespaces auto-import members
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
