Namespaces
A namespace is TypeScript's older way to group values and types under one name, written namespace App { ... }. New code should use ES modules instead. Namespaces still appear in .d.ts files and in declaration merging.
Detailed Explanation
Before ES modules were standard, TypeScript invented namespace (originally module) so a large program could avoid putting everything in the global scope. A namespace is both a value and a type container. You can nest them, export from them, and merge two namespace App blocks in the same scope into one.
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);
}
}
Geometry.Point is a type. Geometry.distance is a function. At runtime, a namespace becomes an object (an IIFE that assigns to a var), unless it is declare namespace and exists only in types.
ES modules replaced that job. export interface Point and export function distance in a file, then import { distance } from "./geometry.js", is the form every current handbook recommends. Namespaces do not participate in module resolution, tree-shaking is worse, and they encourage a single global bag of names.
Where namespaces are still the right tool:
- Augmenting a library that exposes a global (
declare namespace NodeJS { interface ProcessEnv { ... } }). - Grouping types in a
.d.tsthat must match an existing UMD or global script. - Declaration merging with a function or class:
function parse(input: string): ParseResultplusnamespace parse { export type ParseResult = ... }soparse.ParseResultworks. The compiler supports this merge; it is how some APIs attach types to a value.
export as namespace Foo in a .d.ts is the UMD pattern: the module can be imported, and in a script it is also the global Foo. You will see it in older @types packages.
Do not start a new application file with namespace. Do not mix namespace and import/export in the same file without knowing you just made it a module and changed the merge rules. A file with a top-level import or export is a module; a namespace inside it is a local object, not a global merge target.
If you are converting old code, each namespace file becomes a module, export stays, and consumers switch from App.foo to an import. The types do not have to change much. The runtime object that used to be the namespace goes away.
Code Examples
1. The legacy grouping form.
// 1. The legacy grouping form.
namespace Geometry {
export type Point = { x: number; y: number };
export function origin(): Point {
return { x: 0, y: 0 };
}
}
const p: Geometry.Point = Geometry.origin();
console.log(p.x);2. The module form you should write instead.
// 2. The module form you should write instead.
export type Point = { x: number; y: number };
export function origin(): Point {
return { x: 0, y: 0 };
}3. A function/namespace merge, still used in declarations.
// 3. A function/namespace merge, still used in declarations.
function createId(prefix: string): createId.Id {
return `${prefix}_${Date.now()}` as createId.Id;
}
namespace createId {
export type Id = string & { readonly brand: "Id" };
}
const id: createId.Id = createId("user");Common Gotchas
A file with `import`/`export` is a module
`namespace App` inside it will not merge with `namespace App` in another module. That is the usual "I added an import and my namespace vanished" bug.
`namespace` is not an ES module
you cannot `import Geometry from "./geometry"` unless that file also has a default export. Prefer real exports.
`declare namespace` in a `.d.ts` is ambient and can merge
this is still how Node and DOM typings organize globals. Application `.ts` files should not copy that style.
Nested namespaces emit nested objects
they are harder to tree-shake than ESM named exports. Conversion is worth it for bundle size as well as clarity.