Wednesday, July 3, 2024
HomeLanguagesJavascriptWhat is namespace in Typescript ?

What is namespace in Typescript ?

In this article, we are going to see what is namespace in Typescript. In TypeScript, a namespace is a way to organize code into logical groups and avoid naming collisions between identifiers. Namespaces provide a way to group related code into a single namespace or module so that we can manage, reuse and maintain our code easily.

Benefits of Using Namespaces:

  • Logical grouping: Namespaces provide a way to group related code into a single namespace or module, making it easier to manage and maintain your code.
  • Avoid naming collisions: Namespaces help to avoid naming collisions between identifiers by providing a unique namespace for each piece of code.
  • Encapsulation: Namespaces provide a way to encapsulate code by hiding implementation details and only exposing the public API.
  • Modularity: Namespaces provide a way to create modular code by breaking up a large codebase into smaller, more manageable pieces.

How to declare Namespace: In the code snippet written below, we have declared a namespace called MyNamespace. We have also defined a function called myFunction inside the namespace. The export keyword is used to make the function accessible outside the namespace.

namespace MyNamespace {
    export function myFunction() {
        console.log('This is my function');
    }
}

How to Access the Namespace: To access a function inside a namespace, you can use the dot notation to specify the namespace and function name. in the code below we can see that. we are accessing the myFunction function inside the MyNamespace namespace.

MyNamespace.myFunction(); // Output: This is my function

Example 1: In this example, we have created a simple namespace called MyNamespace with a single function called myFunction. We have also used the export keyword to make the function accessible outside the namespace. Finally, we have called the myFunction function outside the namespace using the dot notation.

Javascript




namespace MyNamespace {
    export function myFunction() {
        console.log('This is my function in MyNamespace');
    }
}
  
// Output: This is my function in MyNamespace
MyNamespace.myFunction();


Output:

"This is my function in MyNamespace" 

Example 2: In this example, we have created a nested namespace called MySubNamespace inside the MyNamespace namespace. We have also defined a function called myFunction inside the MySubNamespace namespace. We have used the export keyword to make the function accessible outside the namespace. Finally, we have called the myFunction function outside the namespace using the dot notation with both the parent and child namespace names.

Javascript




namespace MyNamespace {
    export namespace MySubNamespace {
        export function myFunction() {
            console.log('This is my function in MySubNamespace');
        }
    }
}
  
// Output: This is my function in MySubNamespace
MyNamespace.MySubNamespace.myFunction();


Output: 

"This is my function in MySubNamespace" 

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments