0
0
Typescriptprogramming~5 mins

Namespace declaration in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Anamespace
Bmodule
Cpackage
Dclass
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
How do you call a function named sayHi inside a namespace Greetings?
AsayHi.Greetings()
BsayHi()
CGreetings->sayHi()
DGreetings.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
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
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.