Skip to content

Lesson 9 of 9 · 27m

Migrating Untyped React Incrementally

Convert a JavaScript React codebase without stopping feature work: boundary-first typing, allowlists, enforcement, and strangler-fig refactoring.

Rewrites fail because the business does not pause while you make the code aesthetically pleasing. Successful TypeScript migrations are incremental, slightly boring, and operationally strict: new code typed, old code converted at the boundaries, no regressions allowed.

Establish the beachhead

Start with allowJs and a small typed core:

{
  "compilerOptions": {
    "allowJs": true,
    "checkJs": false,
    "strict": true,
    "outDir": "dist"
  },
  "include": ["src"]
}

Rename leaf utilities and small components first — not because they are important, but because they expose the type shapes everything else needs. Shared API response types, formatting utilities, and design-system primitives are the real first targets.

Type the data boundary before the components

export type Invoice = {
  id: string;
  customerName: string;
  totalCents: number;
  status: "draft" | "open" | "paid" | "void";
};

If your API types are wrong, every component type downstream is theater. Generate types from the backend schema when possible (openapi-typescript, database codegen, or a shared contract package). Hand-maintained API types rot quickly.

Convert by vertical slice, not by file type

Do not spend a month converting every .js file in components/. Pick one feature — settings, billing, search — and convert route, hooks, API functions, components, and tests together. The feature becomes proof that typed code can ship, and it reveals migration friction in one contained place.

Put temporary types in quarantine

During migration you will need pragmatic escape hatches:

// src/types/legacy.ts
export type LegacyUserRecord = {
  id: string;
  name?: string; // old API occasionally omitted this before 2024-10
};

Name them honestly and keep them out of your clean domain types. unknown plus a validation function is safer than a fantasy type invented by reading one payload.

Prevent backsliding mechanically

  • CI runs tsc --noEmit on every pull request.
  • Lint bans new any outside migration quarantine files.
  • Ratchet counts are fine when used honestly: a checked-in baseline that may only shrink.
  • New files must be .ts/.tsx unless there is a written reason.

Refactor only after types tell the truth

Type first, reshape second. Once the compiler can see the code, then split components, remove booleans, and introduce unions. If you redesign during the rename, every error becomes ambiguous: migration bug, refactor bug, or both?

Field notes

  • Do not convert tests last; typed tests catch incorrect assumptions about the migration itself.
  • A file full of any is sometimes a valid temporary bridge, but it should be visibly temporary and tracked.
  • Expect backend payloads to lie; runtime validation at the edge is not optional in serious systems.
  • Finish lines matter. "99% converted" means the worst 1% still controls your refactoring confidence.

Was this lesson helpful?

Sign in to rate