Sling Academy
Home/TypeScript/Namespaces in TypeScript: A Complete Guide

Namespaces in TypeScript: A Complete Guide

Last updated: January 07, 2024

Introduction

TypeScript namespaces are a way to organize code into hierarchical groupings and manage named objects to avoid naming collisions. This guide provides a detailed overview of using namespaces with practical code examples.

Understanding Namespaces

In TypeScript, namespaces are used to group logically related code constructs like interfaces, classes, functions, and variables. Namespaces can be considered as a ‘module’ that contains its internal logic separated from the outer scope, thus preventing global namespace pollution and name conflicts.

namespace MyNamespace {
    export class MyClass {}
    export function myFunction() {}
    export const myVariable = 42;
}

Basic Namespace Syntax

Declaring a namespace in TypeScript is straightforward using the namespace keyword. You then place the related code inside curly braces, prefacing exports with the export keyword so they can be accessible outside the namespace.

Nesting Namespaces

Namespaces can be nested within each other for further organization and hierarchical structuring.

namespace Outer {
    export namespace Inner {
        export class NestedClass {}
    }
}

Splitting Namespaces Across Files

Large codebases may require splitting a namespace across multiple files for better maintainability. You can do this using the // /// directive at the top of your files:

// File: MyNamespace_Part1.ts
namespace MyNamespace {
    export class MyClass {}
}

// File: MyNamespace_Part2.ts
///
namespace MyNamespace {
    export function myFunction() {}
}

Using Namespaces

Once you have defined your namespaces and the structure within, you can use the namespaced entities by their fully qualified names.

let instance = new MyNamespace.MyClass();
MyNamespace.myFunction();

Aliasing

Sometimes, using fully qualified names is verbose, so TypeScript allows aliasing:

import MyClass = MyNamespace.MyClass;
let instance = new MyClass();

Merging Namespaces with Functions/Classes

TypeScript also supports an advanced feature where namespaces can be merged with functions and classes.

function MyFunction() {}
namespace MyFunction {
    export const meaning = 42;
}

// Example usage:
MyFunction();
console.log(MyFunction.meaning);

Namespaces vs. Modules

While namespaces are powerful, TypeScript much favors modules for code organization. Unlike namespaces, modules do not require export declarations and work across files naturally.

// myModule.ts
export class MyClass {}

// otherFile.ts
import { MyClass } from './myModule';

Transitioning to modules is recommended when working on a module-based system such as Node.js or front-end frameworks that use bundlers like Webpack.

Best Practices

Although namespaces are not commonly used in modern TypeScript development due to the adoption of modules, there are scenarios where they are useful. It’s important to adhere to best practices when using them:

  • Be consistent with namespacing conventions across your project.
  • Prefer modules over namespaces when working with a module loader.
  • Avoid deep nesting of namespaces to maintain code readability.
  • Use aliasing to shorten lengthy namespaced references where appropriate.

Conclusion

TypeScript namespaces provide a useful tool for grouping related code entities and avoiding naming collisions. While they can be useful in specific cases, modern TypeScript development trends towards the use of modules. Understanding how and when to utilize namespaces remains a valuable skill for TypeScript developers.

Next Article: Using Multiple Types in an Array in TypeScript

Previous Article: Type Declaration Files in TypeScript: A Complete Guide

Series: The First Steps to TypeScript

TypeScript

You May Also Like

  • TypeScript: setInterval() and clearInterval() methods (3 examples)
  • TypeScript sessionStorage: CRUD example
  • Using setTimeout() method with TypeScript (practical examples)
  • Working with window.navigator object in TypeScript
  • TypeScript: Scrolling to a specific location
  • How to resize the current window in TypeScript
  • TypeScript: Checking if an element is a descendant of another element
  • TypeScript: Get the first/last child node of an element
  • TypeScript window.getComputerStyle() method (with examples)
  • Using element.classList.toggle() method in TypeScript (with examples)
  • TypeScript element.classList.remove() method (with examples)
  • TypeScript: Adding Multiple Classes to An Element
  • element.insertAdjacentHTML() method in TypeScript
  • TypeScript – element.innerHTML and element.textContent
  • Using element.removeAttribute() method in TypeScript
  • Working with Document.createElement() in TypeScript
  • Using getElementById() method in TypeScript
  • Using Window prompt() method with TypeScript
  • TypeScript – window.performance.measure() method (with examples)