0
0
Typescriptprogramming~15 mins

Declaration merging for namespaces in Typescript - Mini Project: Build & Apply

Choose your learning style9 modes available
Declaration merging for namespaces
📖 Scenario: Imagine you are organizing a library system. You want to group related information about books and authors using namespaces. Sometimes, you need to add more details to the same namespace without rewriting it.
🎯 Goal: You will create two separate namespace declarations with the same name and see how TypeScript merges them into one. This helps keep your code organized and extendable.
📋 What You'll Learn
Create a namespace called Library with a function getBookTitle that returns a string.
Add a second Library namespace with a function getAuthorName that returns a string.
Call both functions and print their results to see the merged namespace in action.
💡 Why This Matters
🌍 Real World
In large projects, you often want to split code into parts but keep related things grouped. Declaration merging lets you add to the same namespace from different files or sections.
💼 Career
Understanding declaration merging helps you maintain and extend TypeScript codebases, especially when working with libraries or modular code.
Progress0 / 4 steps
1
Create the first Library namespace
Create a namespace called Library with a function getBookTitle that returns the string "The Great Gatsby".
Typescript
Need a hint?

Use namespace Library { export function getBookTitle() { return "The Great Gatsby"; } }

2
Add a second Library namespace
Add another namespace called Library with a function getAuthorName that returns the string "F. Scott Fitzgerald".
Typescript
Need a hint?

Write a second namespace Library block with the new function inside.

3
Call both functions from the merged Library namespace
Write code to call Library.getBookTitle() and Library.getAuthorName(), storing their results in variables title and author.
Typescript
Need a hint?

Use const title = Library.getBookTitle(); and const author = Library.getAuthorName();

4
Print the results
Print the variables title and author using two separate console.log statements.
Typescript
Need a hint?

Use console.log(title); and console.log(author); to show the results.