📖 Scenario: Imagine you are organizing a small library of books. You want to group related functions and variables under a common name to keep things tidy and avoid name clashes.
🎯 Goal: You will create a Library namespace that contains a list of books and a function to display the books.
📋 What You'll Learn
Create a namespace called Library
Inside Library, create a variable books which is an array of strings
Inside Library, create a function showBooks that prints each book name
Call the showBooks function outside the namespace to display the books
💡 Why This Matters
🌍 Real World
Namespaces help organize code in large projects by grouping related code together and avoiding name conflicts.
💼 Career
Understanding namespaces is important for working on TypeScript projects, especially when collaborating with others or managing big codebases.
Progress0 / 4 steps
1
Create the Library namespace with a books array
Create a namespace called Library and inside it create a variable called books which is an array containing these exact strings: 'The Hobbit', '1984', 'Brave New World'.
Typescript
Hint
Use namespace Library { } to start. Inside, declare export const books as an array of strings.
2
Add the showBooks function inside Library
Inside the Library namespace, add a function called showBooks that loops over the books array and prints each book name using console.log.
Typescript
Hint
Define export function showBooks(). Use a for...of loop to print each book.
3
Call the showBooks function outside the namespace
Outside the Library namespace, call the showBooks function using the namespace prefix to display the list of books.
Typescript
Hint
Use Library.showBooks() to call the function outside the namespace.
4
Display the list of books using the namespace function
Run the program to display the list of books. The output should show each book name on its own line.
Typescript
Hint
Check the console output. You should see each book name printed on its own line.
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.