Recall & Review
beginner
What is a namespace in TypeScript?
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.<br>Example:<br>
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?
✗ Incorrect
The
namespace keyword is used to declare a namespace in TypeScript.How do you make a function inside a namespace accessible outside it?
✗ 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?✗ Incorrect
You call it by prefixing with the namespace name:
Greetings.sayHi().Can namespaces help avoid name conflicts in large projects?
✗ Incorrect
Namespaces group related code and prevent name conflicts by creating unique scopes.
Are namespaces the same as ES6 modules in TypeScript?
✗ 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.