Sub topic 1
Plan the migration: why and how to go file by file
This chapter covers the decisions you make before you touch any code. It explains why a file-by-file migration works, what order to convert files in, and how to tell when a file is done. Later chapters cover the commands and the compiler errors in detail.
Prerequisites
This chapter assumes you have:
- Working knowledge of modern JavaScript: functions, objects, arrays, and either ES modules or CommonJS.
- Node.js and npm installed, and comfort running commands in a terminal.
- A basic idea of what static types are: labels the compiler checks before the program runs.
You do not need to know TypeScript syntax yet. Annotations, interfaces, and tsconfig.json options are introduced in later chapters.
Why migrate at all
TypeScript is JavaScript with a static type checker added. The checker reads your code, infers or reads the types, and reports mismatches before you run anything. Typical benefits in an existing codebase:
- Earlier errors. Misspelled property names, wrong argument counts, and calls on values that might be
undefinedshow up in the editor, not in production. - Safer refactoring. Renaming a function or changing an object's shape produces a list of every place that must change.
- Better editor support. Autocomplete and hover information come from the types.
- Documentation that stays accurate. A type signature cannot drift from the code the way a comment can.
There are costs too. The build gains a compile step. Contributors must learn the type system. Some patterns that are easy in JavaScript, such as functions returning different shapes depending on their arguments, take effort to describe. Decide up front that these costs are worth paying for this codebase.
Why go file by file
A big-bang rewrite, where you convert everything in one branch, is risky. The branch lives for weeks, merge conflicts pile up, and a single compile failure blocks every other change. You also learn what your codebase needs from the type system only after most of the work is done.
TypeScript supports a different approach. The compiler can process .js and .ts files in the same project, so you can convert one file, ship it, and move on. Each step is small and reversible, and the application keeps working throughout.
The tools that make this possible are covered in the next chapters:
allowJslets the compiler read and emit JavaScript files alongside TypeScript.checkJsand// @ts-checkapply type checking to JavaScript files without renaming them.- Renaming
.jsto.tsis the final step for each file, not the first.
Set goals before you start
A migration without a stopping point tends to stall at 60 percent. Write down what "done" means. Three common targets:
| Target | What it means | Good fit when |
|---|---|---|
| Tooling only | Compiler runs, editor shows types, most code is still `.js` | You want editor help with almost no risk |
| Core converted | Shared modules and the data layer are `.ts`; leaf scripts stay `.js` | The team is small or time is limited |
| Fully strict | Every file is `.ts` and `strict` is on | The project is long-lived and actively changed |
You can move from one target to the next later. Picking the first one explicitly keeps the effort bounded.
Choose a conversion order
The order matters because types flow from the files a module imports. If orders.js imports money.js, then typing money.js first gives orders.js real types to check against. Converting in the other order means orders sees untyped imports and gets little benefit.
Two orders work well:
- Leaves first. Start with files that import nothing from your own project: utilities, constants, pure helper functions. They are small, have few dependencies, and their types spread upward to the files that use them.
- Hot spots first. Start with the files that change most often or cause the most bugs. The payoff is immediate, but these files often import many untyped modules, so expect more
anyearly on.
For a first migration, choose leaves first. It builds your sense of the process on low-risk code. Once the shared helpers are typed, move to the hot spots.
The cycle for each file
Treat every file as one small, repeatable cycle. Later chapters teach each step.
- Make sure the project builds and its tests pass before you begin.
- Optionally add
// @ts-checkto the JavaScript file and fix what the compiler reports, using JSDoc types. - Rename the file from
.jsto.ts. - Read the compiler errors and sort them into quick fixes and real design questions.
- Add annotations and interfaces until the file compiles.
- Run the tests and the application.
- Commit the file by itself.
Step 7 is worth following. A commit that only renames and annotates one file is easy to review and easy to revert.
Decide how strict to be, and when
The compiler's strictness is controlled by tsconfig.json. A common mistake is to turn on strict immediately. In a large JavaScript codebase that can produce hundreds of errors at once, and the list tells you nothing about priorities.
A better plan is to start with strict set to false. This is the setting used in the starter configuration in the next chapter. The compiler still catches real mistakes, such as calling a function that does not exist, but it does not demand annotations everywhere. You then enable individual checks one at a time, such as noImplicitAny and strictNullChecks, and fix the errors each one reveals. The final chapters return to this and finish with the full strict setting.
This keeps each pull request small. It also means the strictness of the codebase only increases, which is easy to enforce later in CI.
Common pitfalls in planning
- Converting everything to
anyto get green. A file full ofanycompiles but gives no protection. Use it as a temporary marker, and track those places so you can return to them. - Mixing a rewrite with the migration. If you change behavior while adding types, a failing test could come from either change. Keep behavior identical during conversion.
- Skipping the tests. Types do not prove logic is correct. Tests are what tell you a conversion did not change behavior.
- No owner or deadline for each target. Without one, migrations drift. Track progress by counting remaining
.jsfiles, or by countinganyoccurrences over time. - Ignoring the build. Once you emit from TypeScript, the code that runs is the compiled output. Your start script, test runner, and deployment must point at it. The build chapter covers this.
A minimal plan you can copy
For a small Node.js project, a realistic plan looks like this:
- Install the compiler and create
tsconfig.jsonwithallowJson andstrictoff. - Confirm the existing tests pass through the new build.
- Convert utility files, one per commit.
- Convert modules that depend on those utilities.
- Install
@typespackages for third-party libraries as they come up. - Enable
noImplicitAny, fix errors, then enablestrictNullChecks, fix errors. - Switch to full
strictand add a type-check step to CI.
Exercise
Your project has a utils.js that imports nothing local, and a report.js that imports utils.js and three other untyped modules. You are starting the migration. Which file do you convert first, and why? Name one risk if you started with the other.
Show answer
Convert utils.js first. As a leaf module, it has no local dependencies, so it is small and low risk, and its types become available to report.js once it is converted. If you started with report.js, its imports from untyped modules would be treated loosely, so you would get little checking and would add many temporary any annotations that you must revisit later.
Exercise
A teammate proposes setting "strict": true in tsconfig.json on the first day of the migration, in a single branch that converts all 80 files. Give two reasons to prefer the plan in this chapter.
Show answer
First, enabling strict immediately can produce hundreds of errors at once, with no way to prioritize them, and it blocks the branch from merging. Second, a single large branch accumulates merge conflicts and cannot be shipped until every file compiles. Converting file by file with strict off, then enabling checks one at a time, keeps each change small, reviewable, and reversible.