Modules vs Namespaces
The shift to standard ES Modules.
The Evolution of Scope
Before JavaScript had standard import/export syntax (ES Modules), TypeScript invented its own module system called namespace.
Today, standard ES Modules are natively supported by Node.js and all modern browsers, making TS namespaces largely obsolete.
Why Switch?
Namespaces compile down to instantly-invoked function expressions (IIFEs) attached to the global object. This makes tree-shaking (removing unused code during build) nearly impossible for modern bundlers like Vite or Next.js.
ES Modules use a static structure that bundlers can analyze easily.
Syntax
typescript
// ❌ Legacy Namespaces
namespace Geometry {
export function area(r: number) { return Math.PI * r * r; }
}
/// <reference path="geometry.ts" />
Geometry.area(5);
// ✅ Modern ES Modules
export function area(r: number) { return Math.PI * r * r; }
import { area } from "./geometry.js";
area(5);
Try it
Compare the legacy namespace syntax alongside its compiled JavaScript output against the clean, modern ES Module standard.
(function (_MathUtils) {
function add(a) {}
_MathUtils.add = add;
})(MathUtils || (MathUtils = {}));
Check yourself
Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.
Remember this
- TypeScript
namespaceis a legacy feature from before ES Modules existed. - Always use standard
import/exportsyntax in modern apps. - Namespaces break tree-shaking and bloat your bundle size.
- Only use namespaces in
.d.tsfiles to type legacy global scripts.
Done with this concept?
Mark it complete to track your progress. No login needed.