Lesson

TypeScript Namespaces

10 min reading
Free Course

TypeScript Namespaces vs. ES Modules: Modern Organization Rules

TypeScript namespaces (originally called internal modules) wrap functions, interfaces, and types inside a named global or nested scope using the namespace keyword. Before ES2015 standardized import and export, namespaces prevented global variable collisions across browser scripts.

In modern TypeScript projects, ES modules (import/export) are strongly preferred over namespaces for application code because bundlers tree-shake ES modules cleanly and single-file compilers support them reliably.

Namespace Syntax vs. Standard ES Modules

If you encounter namespaces in legacy codebases or global .d.ts files, each exported member inside the block needs export:

namespace Geometry {
  export interface Point {
    x: number;
    y: number;
  }

  export function distance(a: Point, b: Point): number {
    return Math.hypot(b.x - a.x, b.y - a.y);
  }
}

console.log(Geometry.distance({ x: 0, y: 0 }, { x: 3, y: 4 }));

In modern code, put Point and distance in geometry.ts using standard export and import them via import * as Geometry from "./geometry".

Where Namespaces Are Still Valid

Namespaces remain useful inside ambient .d.ts declaration files describing global browser SDKs (such as declare namespace GoogleAnalytics) or attaching sub-types to a class or function (declare namespace Express).

Common Mistakes to Avoid

  • Wrapping ES module files inside namespace App { ... }: Mixing import/export with an outer namespace creates redundant nesting and breaks tree-shaking.
  • Using triple-slash /// <reference path="..." /> in modern apps: Triple-slash references were for pre-module script concatenation; use standard ES import statements instead.
  • Using runtime namespaces with Node's --experimental-strip-types: TypeScript namespaces that emit code are rejected by lightweight type-stripping runtimes.

Frequently Asked Questions

Should I use namespace in a new React, Next.js, or Node.js project?

No. Use standard ES modules (export and import) across separate files, and use import * as Helpers from "./helpers" if you want grouped dot-access.

What is the difference between module and namespace keywords in older TS?

Older TypeScript wrote module GeoMath {} for internal namespaces; modern TypeScript renamed that to namespace GeoMath {} so module refers strictly to ES/external module declarations (declare module "pkg").

Can I split one namespace across multiple files?

Technically yes, though bundling multiple global-script files is legacy; ES modules with barrel exports (index.ts) are cleaner and supported by all modern bundlers.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
TypeScript Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum