Course contentsShow
← TypeScript
Lesson 1153 of 1,79627. Declaration FilesFree lesson

declare namespace for Legacy Globals

Group related global declarations under a namespace for libraries that export objects globally.

declare namespace for Legacy Globals

What you'll learn: How to organize multiple global declarations from legacy libraries into a clean, structured namespace.

What Problem Does This Solve?

Many older JavaScript libraries expose their functionality through a single global object with multiple properties and methods. For example, a library might attach everything to window.MyLib with various subproperties. Rather than declaring each piece separately with scattered declare var and declare function statements, you can group them together under a declare namespace.

The Pattern in Plain English

Think of a declare namespace like creating a filing cabinet for global declarations. Instead of having loose papers (individual declarations) scattered everywhere, you put related items into one labeled drawer. The namespace groups all related global types, functions, and variables under one umbrella name.

Basic Syntax

declare namespace MyLibrary {
  function doSomething(value: string): void;
  function doSomethingElse(count: number): number;
  
  const VERSION: string;
  
  interface Options {
    enabled: boolean;
    mode: string;
  }
}

This tells TypeScript that somewhere in the global scope, there's an object called MyLibrary with those methods, properties, and types available.

When to Use This

Use declare namespace when:

  • A library exposes a single global object with multiple members
  • You're typing legacy code that uses this pattern
  • You want to organize related global declarations together

The namespace keeps your declaration file readable and mirrors the structure developers actually use when calling the library.

Key Takeaway: declare namespace groups related global declarations into a structured container, making it easier to type legacy libraries that expose everything through a single global object.