Skip to the solution
MediumFrontend12 min read

How do you migrate a JavaScript codebase to TypeScript?

661 views

The problem

Question presented to candidate: "You join a team with a large JavaScript codebase that is still shipping features every week. They want to move to TypeScript. How would you plan and run that migration without stopping the team?"

What a strong answer should cover:

  • Incremental, not big-bang: allowJs lets .js and .ts files live in one project, so files can be converted one at a time while the app keeps building
  • Getting value before renaming: checkJs or per-file // @ts-check plus JSDoc types, and // @ts-nocheck to exclude a file for now
  • Order of work: leaf modules and shared utilities first, types at the boundaries (API responses, config, JSON.parse results as unknown), @types/* or a declare module shim for untyped dependencies
  • Strictness as a ratchet: start with what the team can pass, then enable strict (or its flags one by one), tracking escape hatches such as a TSFixMe alias and @ts-expect-error (not @ts-ignore)
  • CI and tooling: run tsc --noEmit in CI because many bundlers only strip types; keep run-time behaviour unchanged during conversion
  • What the conversion typically surfaces: missing arguments, possibly-undefined values, typos, string/number mix-ups; types are compile-time only, so validate external data at run time

Clarifying questions expected:

  • How big is the codebase, how is it built and tested today, and is there a deadline?
  • Which parts change most often or break most often?
  • Is this an application or a library with external consumers who will need .d.ts files?

Code / implementation expected: Optional β€” a tsconfig.json for the mixed phase and one converted module.

  • migration
  • tooling

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

12 min read Β· 11 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

TypeScriptThe bugs a conversion surfaces, and what plain JavaScript does
TypeScriptEscape hatches and boundaries: TSFixMe, unknown JSON, declare global

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 38 of 81 in TypeScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

What do Exclude, Extract, and NonNullable do?

Continue