Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • TypeScript Tutorial
  • TS Exercise
  • TS Interview Questions
  • TS Cheat Sheet
  • TS Array
  • TS String
  • TS Object
  • TS Operators
  • TS Projects
  • TS Union Types
  • TS Function
  • TS Class
  • TS Generic
Open In App
Next Article:
What is namespace in Typescript ?
Next article icon

What is namespace in Typescript ?

Last Updated : 17 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In TypeScript, a namespace is a way to organize code logically and prevent naming conflicts between identifiers. It allows developers to group related functionalities, such as interfaces, classes, functions, and variables, within a dedicated scope.

Namespaces are particularly useful for structuring large applications, avoiding global scope pollution, and maintaining a clear hierarchy.

Declaring and Using a Namespace in TypeScript

Here’s a simple example demonstrating how to create and use a namespace in TypeScript:

JavaScript
namespace Geometry {   export class Circle {     constructor(public radius: number) {}      area(): number {       return Math.PI * this.radius ** 2;     }   } } const circle = new Geometry.Circle(5); console.log(circle.area()); 
  • Namespace Declaration: The Geometry namespace encapsulates the Circle class, ensuring that its members are scoped within this namespace.
  • Class Definition: The Circle class has a constructor to set the radius and a method area() to calculate the area of the circle.
  • Object Instantiation: An instance of Circle is created with a radius of 5.
  • Method Invocation: The area() method is called on the circle instance to compute and log the area.

Output:

78.53981633974483

More Examples of Namespace in TypeScript

Declaring and Accessing a Simple Namespace

JavaScript
namespace MyNamespace {   export function myFunction() {     console.log('This is my function in MyNamespace');   } }  MyNamespace.myFunction(); 
  • Namespace Declaration: MyNamespace is defined using the namespace keyword, encapsulating the myFunction function.
  • Function Definition: myFunction logs a message to the console.
  • Export Keyword: The export keyword makes myFunction accessible outside the MyNamespace namespace.
  • Function Invocation: myFunction is called using dot notation: MyNamespace.myFunction().

Output:

This is my function in MyNamespac

Declaring and Accessing a Nested Namespace

JavaScript
namespace MyNamespace {   export namespace MySubNamespace {     export function myFunction() {       console.log('This is my function in MySubNamespace');     }   } }  MyNamespace.MySubNamespace.myFunction(); 
  • Nested Namespace Declaration: MySubNamespace is defined inside MyNamespace, creating a hierarchical structure.
  • Function Definition: myFunction logs a message to the console.
  • Export Keyword: The export keyword makes myFunction accessible outside the MySubNamespace namespace.
  • Function Invocation: myFunction is called using dot notation: MyNamespace.MySubNamespace.myFunction().

Output: 

"This is my function in MySubNamespace" 

Benefits of Using Namespaces

  • Organize Code: Namespaces help group related code together, making it easier to manage and maintain.
  • Prevent Conflicts: They avoid naming issues by keeping your code in its own unique space.
  • Hide Details: You can keep internal code hidden and only show the necessary parts to the outside world.
  • Break Down Code: Namespaces let you divide a large project into smaller, easier-to-manage pieces.

Namespaces vs. Modules in TypeScript

Here are the main differences between Namespaces and Modules in TypeScript:

FeatureNamespacesModules
UsageTypeScript-specificStandard JavaScript feature
ScopeWorks within a global fileWorks across multiple files
SyntaxUses namespace keywordUses import/export
ModularityLess flexibleMore scalable and reusable
RecommendationNot preferred for new projectsRecommended for modern development

Next Article
What is namespace in Typescript ?

I

iamgaurav
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • TypeScript

Similar Reads

    What is Type Predicates in Typescript ?
    In this article, we are going to learn about the type predicates in Typescript. TypeScript is a statically typed programming language that provides many features to make your code more efficient and robust. Type predicates in TypeScript are functions that return a boolean value and are used to narro
    3 min read
    What are TypeScript Interfaces?
    TypeScript interfaces define the structure of objects by specifying property types and method signatures, ensuring consistent shapes and enhancing code clarity.Allow for optional and read-only properties for flexibility and immutability.Enable interface inheritance to create reusable and extendable
    4 min read
    What’s new in TypeScript 5.4 ?
    TypeScript 5.4 brings forth several new capabilities and refinements, aiming to enhance developer productivity and code quality. Some of the new capabilities that are added or Introduced in TypeScript 5.4 are as follows: Table of Content Variadic Tuple TypesClass Property Inference from Constructors
    2 min read
    What is TypeScript Definition Manager ?
    What is TypeScript Definition Manager? TypeScript Definition Manager (TSDM) is a type definition manager for TypeScript, the popular JavaScript superset. It is a command line tool (CLI) that helps developers create, maintain, and manage type definitions for TypeScript projects. It is an open-source,
    4 min read
    What is Type Erasure in TypeScript?
    TypeScript is a very mighty superset of JavaScript, which adds static typing to the language and allows developers to catch errors early on as well as write more maintainable code. This being said a TypeScript type system erases type information at compile time (or during the compilation), a phenome
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences