Docs
/
TypeScript
Chapter 16
16 — Modules & Namespaces
Core Concepts
- ES Modules —
import/exportsyntax (the modern standard) - Named exports —
export function fn(),export { a, b } - Default exports —
export default class Foo - Re-exports —
export { fn } from "./module"barrel files - Dynamic imports —
const mod = await import("./module")code splitting - Namespaces —
namespace Foo { }legacy TS-specific grouping - Module augmentation — extend existing module types
- Side-effect imports —
import "./polyfill"just run the module
Module Best Practices
- Use named exports (better tree-shaking, autocomplete)
- Use barrel files (
index.ts) sparingly (can hurt tree-shaking) - Prefer ES modules over namespaces in modern code
- Use
typeimports for type-only imports:import type { User } from "./types"