EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Software Development Software Development Tutorials TypeScript Tutorial TypeScript namespace

TypeScript namespace

TypeScript namespace

Introduction to TypeScript namespace

The following article provides an outline for TypeScript NameSpace. The Namespace comes up with a way of grouping functionalities logically. A logical grouping of functionalities is how typescript organizes and encapsulates the features that share the same characteristics throughout the flow. Basically, it encapsulates the objects sharing the same relationship in the script. It has different modules such as Interface, class, and Function that support this functionality in typescript. They are inbuilt into TypeScript means any declaration will go on the global scope variable. They are also called internal Modules. Naming collisions are resolved using the Typescript Namespaces.

Syntax

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

The syntax for TypeScript NameSpace is :-

A namespace can be created using the keyword namespace by the namespace name.

A curly brace is used that contains the information inside that. Be it Class, Interface, etc.

Syntax:

Namespace <name>
{
//Interface , class , etc.
}

Working of TypeScript namespace

The NameSpace provides the space to encapsulate the logical data by writing it in a function. A namespace with a unique name makes a logical grouping of functions with the desired functions, making it accessible to use throughout TypeScript.

When these function, interfaces that we write inside that namespace provides with a logical meaning by combination and those can be used to write down the code line.

We use the keyword Export that exports the namespace function, making it visible outside the scope of the namespace. Once using the export statement, we can place it anywhere in the code and can work accordingly.

namespace abc {
export function Uppercase(str: string): string {
return str.toUpperCase();
}
}

Using export will have this reference outside the scope of TypeScript.

Output:

declare namespace abc {
function Uppercase(str: string): string;
}

ScreenShot:

Without Using export:

TypeScript namespace output 1

Using Export to change the scope:

TypeScript namespace output 2

To use the namespace components in another place, we first need to include the namespace using a pattern, i.e., inserting the reference path with ///.

We can also have a nested namespace; this allows us to define one namespace into another. We can access the member of the nested namespace using the dot(.) operator.

Example:

namespace Demo {
export namespace NestedDemo {
export class NestedClass {
public function1(p: number) {
return p + .60;
}
}
}
}

ScreenShot:

TypeScript namespace output 3

Example

Let us see TypeScript NameSpace with some Example:-

Let us now create an interface in TypeScript with the name of Demo1.

This interface will have a method that can be used in the post or outside the scope of Typescript.

interface Demo1 {
isTrue(s1: string): boolean;
}

Let’s create a Regex variable.

let Regex1 = /^[A-Za-z]+$/;
let Regex2 = /^[0-9]+$/;

Create a class that implements the interface in the same namespace.

Demo2 is the name of the first class, and Demo3 being the name of the second one.

class Demo2 implements Demo1 {
isTrue(s1: string) {
return Regex1.test(s1);
}
}

The first one checks the String with the regex, whether true or not, and the latter one checks it with a value.

class Demo3 implements Demo1 {
isTrue(s1: string) {
return s1.length === 5 && Regex2.test(s1);
}
}

Screenshot:

output 4

Now let us create an object of the classes and check whether the string given input is matched or not.

We will make the object of both classes. And then, the object name is matched with the input, and the result is printed.

A for loop will help us match the elements in this.

let Demo4: { [s1: string]: Demo1 } = {};
Demo4["Number"] = new Demo3();
Demo4["Letters"] = new Demo2();
let sampleTest = ["This", "9333", "333"];
for (let s1 of sampleTest) {
for (let name in Demo4) {
let Matched = Demo4[name].isTrue(s1);
console.log(`'${s1}' ${Matched ? "matches" : "no match"} '${name}'.`);
}
}

Screenshot:

output 5

This will print the matched elements in the console log of the type Script.

Output:

[LOG]: "'This' no match 'Number'."
[LOG]: "'This' matches 'Letters'."
[LOG]: "'9333' no match 'Number'."
[LOG]: "'9333' no match 'Letters'."
[LOG]: "'333' no match 'Number'."
[LOG]: "'333' no match 'Letters'."

Full Code ScreenShot:

output 6

From this, we saw how a namespace works in TypeScript.

Rules and Regulation for namespace

Let us look over the rules and regulations required for writing a namespace in TypeScript.

  1. A namespace should have a unique name associated with it.
  2. It must start with the keyword namespace.
  3. It contains interfaces, functions giving a logical meaning.
  4. Use the export Keyword to use the function outside the scope of namespace.
  5. We can also merge namespace with another type of Decelerations. We can merge them with class, function, enum.
  6. The codes are organized in a TypeScript NameSpace.
  7. Namespaces are inbuilt in type Script, so variables are into the global scope.
  8. A namespace can span in multiple files.
  9. It removes the naming collisions.

We can also split the codes and encapsulate the code in TypeScript.

From the above article, we saw the rule for writing the code in TypeScript. 

Conclusion

From the above article, we saw the use of NameSpace in TypeScript. We tried to understand how the namespace function works in TypeScript and what are uses at the programming level from various examples and classification.

We also saw the internal working and the advantages of having a namespace that we define for various programming purposes. Also, the syntax and examples helped us to understand much precisely the function.

Recommended Articles

We hope that this EDUCBA information on “TypeScript namespace” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. Typescript for loop
  2. TypeScript typeof
  3. TypeScript Generic
  4. TypeScript Versions
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
Financial Analyst Masters Training Program
1000+ Hours of HD Videos
43 Learning Paths
250+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Data Science Bundle
1500+ Hour of HD Videos
80 Learning Paths
360+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Software Development Bundle
3000+ Hours of HD Videos
149 Learning Paths
600+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
All in One Software Development Bundle3000+ Hours of HD Videos | 149 Learning Paths | 600+ Courses | Verifiable Certificate of Completion | Lifetime Access
Financial Analyst Masters Training Program1000+ Hours of HD Videos | 43 Learning Paths | 250+ Courses | Verifiable Certificate of Completion | Lifetime Access
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more