"Namespaces are old TypeScript. ES modules took their job and did it better."
What namespaces are
TypeScript's namespace keyword groups declarations under a single name within a file or across files (with reference tags). namespace Math { export function add() { ... } } creates a Math namespace; you access members with Math.add(). Multiple namespace Math declarations in the same scope merge.
Namespaces predate ES modules. They were TypeScript's original solution to 'how do I organize code without putting everything in the global scope?' before JavaScript had a standard answer.
Why ESM won
ES modules superseded namespaces because:
- JavaScript standard. ESM is part of the language. Namespaces are TypeScript-only.
- Bundler-friendly. Tree-shaking works on ESM imports; namespaces ship whole.
- File-scoped. ESM's natural unit is the file. Namespaces require explicit grouping.
- Static analysis. Tools (linters, bundlers, IDEs) can analyze imports; namespaces are opaquer.
For new code, never use namespace in .ts source. Use ES modules instead.
Where namespaces still appear
namespace declarations in .d.ts files for legacy libraries: jQuery's types, Lodash's pre-ESM types, large @types/... packages from before the ESM era. You'll read them; you won't write them. Modern library type definitions all use ES modules.
.d.ts files; don't reach for it in your own code.