0
0
Typescriptprogramming~20 mins

Namespace merging in Typescript - Mini Project: Build & Apply

Choose your learning style9 modes available
Namespace Merging in TypeScript
📖 Scenario: Imagine you are organizing a small library system. You want to group related functions and variables under the same name to keep things tidy.
🎯 Goal: You will create two namespaces with the same name Library and merge them. Then you will use the merged namespace to access all the members.
📋 What You'll Learn
Create a namespace called Library with a variable books that holds the number 5
Create another namespace called Library with a function getBooks that returns the value of books
Use the merged Library namespace to call getBooks() and print the result
💡 Why This Matters
🌍 Real World
Namespace merging helps keep related code together, like grouping all library-related tools under one name.
💼 Career
Understanding namespaces and how to merge them is useful for organizing large TypeScript projects in professional software development.
Progress0 / 4 steps
1
Create the first namespace with a variable
Create a namespace called Library and inside it declare a variable books with the value 5.
Typescript
Need a hint?

Use namespace Library { export let books = 5; } to create the namespace and export the variable.

2
Create the second namespace with a function
Create another namespace called Library and inside it declare a function getBooks that returns books from the first namespace.
Typescript
Need a hint?

Remember to export the function and return the variable books inside it.

3
Use the merged namespace
Write a line of code that calls Library.getBooks() and stores the result in a variable called totalBooks.
Typescript
Need a hint?

Use let totalBooks = Library.getBooks(); to call the function and save the result.

4
Print the result
Write a line of code to print the value of totalBooks using console.log.
Typescript
Need a hint?

Use console.log(totalBooks); to show the number of books in the console.