Migrating a JavaScript code base to TypeScript: a step-by-step plan
Most JavaScript code bases that live long enough end up in the same place: nobody is quite sure what shape the data has, refactoring feels risky, and new developers take weeks to become productive. TypeScript fixes much
Most JavaScript code bases that live long enough end up in the same place: nobody is quite sure what shape the data has, refactoring feels risky, and new developers take weeks to become productive. TypeScript fixes much of that, but a badly planned migration can stall development for months.
The good news is that you do not have to stop and rewrite. TypeScript was designed to be adopted gradually, file by file, while the product keeps shipping. This is the plan we follow when clients ask us to move a JavaScript application to TypeScript.
Is it worth it?
For a small script or a prototype that will be thrown away, usually not. For a product that several developers will work on for years, almost always. The benefits show up quickly:
-
Fewer bugs reach users. Whole classes of errors (a misspelled property, a missing argument, a value that can be
undefined) are caught in the editor instead of in production. - Safer refactoring. Rename a field or change a function and the compiler lists every place that needs updating.
- Faster onboarding. Types document what functions expect and return, so new developers spend less time reading code to understand it.
- Better tooling. Autocomplete and inline errors work properly across the whole code base.
The cost is the migration effort and a short learning curve for developers new to TypeScript. Planned well, most teams keep delivering features throughout.
Step 1: Measure where you are
Before changing anything, take stock:
- How many files and lines of JavaScript, and which parts change most often.
- Which framework and build tool you use (React, Vue, Angular, Node.js; Vite, webpack, Next.js), since most now support TypeScript out of the box.
- Which libraries you depend on, and whether they ship their own types or have types on DefinitelyTyped (
@types/βΓΒΆ). - How good your tests are. Tests are your safety net during the migration; if they are thin, add some around the riskiest parts first.
Step 2: Let TypeScript and JavaScript live side by side
Add TypeScript to the project and configure it to accept the existing JavaScript. In tsconfig.json, set allowJs so .js and .ts files can import each other, and start with strict switched off. Make sure the build, the test runner and the linter all understand .ts files. Nothing about the product should change at this point: the goal is simply that both languages compile and run together.
Add a type check to your continuous integration pipeline straight away, so new type errors fail the build instead of piling up.
Step 3: Agree the rules before converting code
- All new files are written in TypeScript, from day one.
- Every file you touch for a feature or fix gets converted, if it is small enough. This spreads the work across normal development.
-
Avoid
any. When you genuinely cannot type something yet, useunknownor a clearly marked temporary type, and track how many remain. -
Shared types live in one place, for example a
typesfolder for API responses and core business objects.
Step 4: Convert from the bottom up
Start with the code everything else depends on, because typing it gives you the most value elsewhere:
- Data models and API responses. Describe the shapes your application works with: users, orders, products. If you have an OpenAPI or GraphQL schema, generate these types instead of writing them by hand.
- Utilities and services. Small, widely used functions are quick to convert and catch many mistakes.
- State management (stores, reducers, context).
- Components and pages, last, once the data they use is typed.
Converting top-down instead (pages first) usually means typing the same data over and over, often slightly differently each time.
Step 5: Tighten the rules gradually
Once most files are converted, switch on stricter checks one at a time rather than all at once. noImplicitAny and strictNullChecks are the two that catch the most real bugs; turn each on, fix what it reports, then move to the next, until strict is fully on. Some teams enable strict mode only for new folders first, which keeps progress visible.
Step 6: Track progress and finish the job
Migrations stall when they become "someday" work. Track a few simple numbers every sprint: the share of files in TypeScript, the count of any and @ts-ignore, and type errors in the build (which should stay at zero). When the last JavaScript files are left, schedule them as real work and finish. Then remove allowJs.
Common mistakes
- The big-bang rewrite. Freezing features to convert everything at once is slow, risky and rarely necessary.
-
Typing everything as
anyto make errors go away. You get the cost of TypeScript without the benefit. - Turning on full strict mode on day one. Thousands of errors at once discourage the team; tighten gradually.
- Hand-writing types for APIs that already have a schema. Generate them, and regenerate when the API changes.
- No tests. Types catch many bugs, but not wrong business logic. Keep the tests that protect it.
How long does it take?
It depends on the size of the code base, test coverage and how much else the team is delivering. A small application can be fully converted in a few weeks alongside normal work. A large product is usually done over a few months, converting as the team works on each area, with the strict settings switched on near the end. The key is that the product keeps shipping the whole time.
How we can help
We provide TypeScript developers and complete teams who build new TypeScript applications and migrate existing JavaScript code bases step by step, without stopping your releases. You interview the developers before they join. See hire TypeScript developers, or estimate your project.
Originally published on the Silver Scintilla blog.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.