Docs
/
TypeScript
Chapter 16

16 — Modules & Namespaces

Core Concepts

  • ES Modulesimport/export syntax (the modern standard)
  • Named exportsexport function fn(), export { a, b }
  • Default exportsexport default class Foo
  • Re-exportsexport { fn } from "./module" barrel files
  • Dynamic importsconst mod = await import("./module") code splitting
  • Namespacesnamespace Foo { } legacy TS-specific grouping
  • Module augmentation — extend existing module types
  • Side-effect importsimport "./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 type imports for type-only imports: import type { User } from "./types"