← Back to Webstore Course

Week 2: TypeScript for Real Code

This week teaches safe refactoring in a production codebase. Emphasis is on type contracts at boundaries: service calls, API payloads, and error responses.

Learning Objectives

  • Replace weak typing at module boundaries.
  • Use narrowing and runtime-safe guards together.
  • Define stable result and error contracts.
  • Refactor without behavior regressions.

Detailed Topics

1. Contract-First Thinking

Teach the student to start from inputs and outputs. Every API or service function should make expected shape and constraints explicit in type definitions.

2. Practical Narrowing Patterns

Cover unknown-to-known transitions, discriminated unions for success and failure states, and common guards for optional values.

3. Refactor Strategy

Make small, testable changes: update one boundary, run tests, confirm behavior parity, then move to the next boundary.

Pair Session Plan (90 Minutes)

  1. Select one medium service module.
  2. Document current weak spots in type safety.
  3. Introduce safer interfaces and response unions.
  4. Run targeted verification and discuss risk reduction.

Independent Ticket

Submit a PR that hardens one component/API helper with stricter types and runtime guards while preserving behavior.

Mentor Checkpoint

  • Student can explain tradeoffs of strictness vs speed.
  • Student can reason about nullable and optional data safely.
  • Student produces readable type names and contracts.
← Previous: Week 1 Next: Week 3 →