A namespace is a way to group related code like variables, functions, and classes under a single name to avoid name conflicts and organize code.
Click to reveal answer
beginner
How do you declare a namespace in TypeScript?
Use the <code>namespace</code> keyword followed by the namespace name and curly braces containing the code to group.<br>Example:<br><pre>namespace MyNamespace {
export function greet() { console.log('Hello'); }
}</pre>
Click to reveal answer
beginner
Why do we use the export keyword inside a namespace?
To make functions, classes, or variables accessible outside the namespace, you must mark them with export. Otherwise, they stay private inside the namespace.
Click to reveal answer
beginner
How do you access a function inside a namespace?
Use the namespace name followed by a dot and the function name. Example:
MyNamespace.greet();
Click to reveal answer
intermediate
Can namespaces be split across multiple files in TypeScript?
Yes, namespaces can be split into multiple files by declaring the same namespace name in each file. When compiled, TypeScript merges them into one namespace.
Click to reveal answer
What keyword is used to declare a namespace in TypeScript?
Anamespace
Bmodule
Cpackage
Dclass
✗ Incorrect
The namespace keyword is used to declare a namespace in TypeScript.
How do you make a function inside a namespace accessible outside it?
AUse the <code>private</code> keyword
BUse the <code>public</code> keyword
CUse the <code>export</code> keyword
DNo special keyword needed
✗ Incorrect
Functions must be marked with export inside a namespace to be accessible outside.
How do you call a function named sayHi inside a namespace Greetings?
AsayHi.Greetings()
BsayHi()
CGreetings->sayHi()
DGreetings.sayHi()
✗ Incorrect
You call it by prefixing with the namespace name: Greetings.sayHi().
Can namespaces help avoid name conflicts in large projects?
AOnly if you use modules
BYes, by grouping code under unique names
COnly if you use classes
DNo, namespaces increase conflicts
✗ Incorrect
Namespaces group related code and prevent name conflicts by creating unique scopes.
Are namespaces the same as ES6 modules in TypeScript?
ANo, namespaces are internal grouping, modules are file-based
BNamespaces are deprecated modules
CYes, they are identical
DModules are a type of namespace
✗ Incorrect
Namespaces are internal code grouping, while modules are based on files and imports/exports.
Explain what a namespace is and why it is useful in TypeScript.
Think about how you keep things organized in a big project.
You got /4 concepts.
Describe how to declare a namespace and access a function inside it.
Remember the syntax and how to reach inside the namespace.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of a namespace in TypeScript?
easy
A. To import external libraries
B. To create a new type of variable
C. To define a function that runs automatically
D. To group related code under one name and avoid name clashes
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 D
Quick 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
A. namespace Utils { }
B. module Utils { }
C. package Utils { }
D. namespace = Utils { }
Solution
Step 1: Recall the correct keyword for namespaces
TypeScript uses the keyword namespace to declare namespaces.
Step 2: Check the syntax format
The correct syntax is namespace Name { }, so namespace Utils { } is correct.
Final Answer:
namespace Utils { } -> Option A
Quick 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
A. undefined
B. 7
C. Error: add is not defined
D. NaN
Solution
Step 1: Understand the namespace and export usage
The function add is inside namespace MathOps and is exported, so it can be accessed outside.
Step 2: Evaluate the function call
Calling MathOps.add(3, 4) returns 3 + 4 = 7.
Final Answer:
7 -> Option B
Quick 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
A. Function getData is not exported, so it cannot be accessed outside
B. Namespace name Data is invalid
C. Syntax error in namespace declaration
D. Console.log is misspelled
Solution
Step 1: Check function accessibility inside namespace
The function getData is declared but not exported, so it's private to the namespace.