TypeScript theorytheory 0/50 · 0%
Tooling · easy

14. Modules: import/export

Sharing code between files with ES module syntax.

TypeScript uses standard ES module syntax. Each file with a top-level `import` or `export` is its own module scope.

ts
// wallet.ts
export interface Wallet { address: string; balance: number; }
export function createWallet(address: string): Wallet {
  return { address, balance: 0 };
}
export default class WalletService { /* ... */ }

// main.ts
import WalletService, { createWallet, Wallet } from "./wallet";

`export type` / `import type` explicitly mark type-only exports, which some build tools use to safely strip them without touching runtime code. Named exports are generally preferred over default exports because they support better auto-import and refactoring tooling.

Check your understanding

  1. 1. What makes a `.ts` file its own module scope?

  2. 2. What does `import type { Wallet } from "./wallet"` signal?

  3. 3. Why are named exports often preferred over default exports?