TypeScript · Types that pay for themselves
How would you migrate a JavaScript codebase to TypeScript?
Short answer
Migrate incrementally: enable allowJs, add checkJs and strictness gradually, type module boundaries first, and fix errors by narrowing rather than casting. Keep the build green and ship in small steps.
A big-bang rewrite creates a long-lived branch and a large review. An incremental migration lets the team keep shipping while the type coverage grows. The compiler can check JavaScript files alongside TypeScript, so the two can coexist during the transition.
{
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"noEmit": true,
"strict": false
},
"include": ["src"]
}A practical sequence
- Add TypeScript and a tsconfig with allowJs and noEmit
- Rename leaf modules or new files first, keeping the build green
- Type public module boundaries and shared data shapes
- Enable strict flags one at a time, starting with noImplicitAny
- Replace casts with narrowing and runtime validation at trust boundaries
- Add type tests for generic helpers and keep tsc in CI
Prioritise code that changes often and code with unclear data shapes. A module that is stable and isolated can stay JavaScript longer. The goal is not 100 percent TypeScript immediately; it is a codebase where new work is typed and the riskiest boundaries are checked.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Why migrate incrementally instead of rewriting?
- 2Which files would you convert first?
- 3How do you handle untyped third-party packages?
- 4How do you introduce strict mode without blocking delivery?
Say it in the interview
I migrate incrementally rather than rewriting everything. I enable allowJs and checkJs, add types at module boundaries first, and turn on strictness in stages. Each step keeps the build green and ships independently. Casts are a last resort; I prefer narrowing and validating so the migration improves real safety rather than just silencing errors.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live TypeScript mock interview and get scored feedback on depth, clarity and confidence.