Modules

Intro to Modules
ES modules are the standard way to organize and share code in modern JavaScript and TypeScript. They let you split code into separate files and import/export functionality between them.
export const menuItem = { name: 'Tea', price: 3.5 }
export function formatMenuItem(item: { name: string; price: number }) {
	return `${item.name}: $${item.price}`
}

// app.ts
import { menuItem, formatMenuItem } from './menu.ts'

Module Benefits

  • Organization - Split code into logical files
  • Reusability - Share code across files
  • Encapsulation - Control what's public vs private
  • Tree-shaking - Bundlers can eliminate unused code
TypeScript fully supports ES modules. Use import and export statements, and TypeScript will handle type checking across module boundaries.
In this exercise, you'll organize code using modules, understand the difference between default and named exports, and practice type-only imports.