TypeScript Modules and Namespaces
Organize code using ES modules and understand when namespaces are still useful.
Large projects need structure. ## ES Modules (preferred) file math.ts: ```ts export function add(a: number, b: number) { return a + b; } ``` file app.ts: ```ts import { add } from "./math"; console.log(add(2, 3)); ``` ## Namespaces (older style) ```ts namespace Utils { export function log(msg: string) { console.log(msg); } } Utils.log("hello"); ``` ## When to use - Use modules for modern apps - Namespaces only for legacy/global scripts ## Graph ```mermaid flowchart LR A[Files] --> B[Exports] B --> C[Imports] ``` ## Remember - Prefer ES modules - Keep code split by feature