The namespaces in TypeScript are used to organize and encapsulate the related functionalities and methods into a particular name code block to reuse them again and again in the code whenever we need the same functionality. There are some key purposes for using namespace in TypeScript as written below:
- Logical collection: It helps in grouping the logically same functionalities into a single code block and organizes logical code.
- Avoiding named collisions: It provides a separate scope and a unique namespace for the identifies which may help in avoiding the name collisions in the code.
- Encapsulation: The code written inside a namespace will be encapsulated just like the classes in TypeScript.
- Code Reusability: The code written inside a namespace can be used anywhere in the whole code just by accessing the values and properties stored inside it.