Sub topic 1
Plan the migration: why and how to go file by file
Prerequisites
Assume you know modern JavaScript, including functions, objects, arrays, and either ES modules or CommonJS. You should have Node.js, npm, and a terminal available. You should also understand that static types describe values before the program runs.
Why migrate incrementally?
A JavaScript-to-TypeScript migration adds checks without requiring a complete rewrite. TypeScript can inspect existing JavaScript, then you can convert one file at a time. This reduces the size of each change and makes compiler errors easier to understand.
An all-at-once conversion often creates too many unrelated problems:
- Every file may produce errors at the same time.
- A change in one module can hide the original cause of an error elsewhere.
- Reviewers cannot easily tell whether a change fixes behavior or only adds types.
- A failed migration is harder to undo safely.
The goal is not to add annotations everywhere immediately. The goal is to create a sequence of small, verifiable changes that preserves the program's behavior.
Choose a migration order
Start with a file that is useful but contained. A small utility module is often a good first target because it has a clear input and output. Avoid starting with the application's entry point, a large stateful module, or a file that depends on many untyped libraries.
Use these questions when choosing the next file:
- Is the file covered by tests or a simple manual check?
- How many other files import it?
- Does it handle data from outside the program?
- Does it use browser, Node.js, or package APIs?
- Can you convert it without changing its public behavior?
A file with many dependents has a larger effect on the project. Converting it early can expose useful type information, but it can also create more errors. A leaf module, which is a module imported by few or no other modules, usually offers a safer first step.
| File candidate | Benefit | Risk |
|---|---|---|
| Small formatting utility | Easy to test and review | May teach little about project-wide patterns |
| Shared data model | Gives many callers better types | Can produce errors across many files |
| Application entry point | Shows the full runtime flow | Usually too large for a first conversion |
Make a migration inventory
Before renaming files, record the current state. List JavaScript files, their importers, their tests, and any third-party packages they use. Mark files that read JSON, environment variables, HTTP responses, or other untyped data. These boundaries deserve extra review because their values are not automatically trustworthy.
Keep runtime changes separate from type changes when possible. First preserve behavior, then improve the type model. Make one conversion per commit or pull request, and run the existing tests after each step.
Define completion for each file
A converted file is ready when it compiles under the current project settings, its imports and exports still work, tests pass, and no unexplained type escape remains. An escape is a deliberate bypass such as any or an unchecked assertion. Record these exceptions so they can be revisited instead of allowing them to spread.
Exercise
Which file is the safest first candidate: a small tested utility with no external input, or the main application entry point?
Show answer
The small tested utility is safer because it has a smaller dependency surface and clearer behavior to verify.