How to Migrate a JavaScript Project to TypeScript Step by Step

You do not migrate a JavaScript project to TypeScript by rewriting it. You do it by letting the two languages live side by side in the same codebase, converting one file at a time, and keeping the app working after every step. This guide shows how to migrate javascript project to typescript in that order: install the tools, teach the compiler to accept your existing code, convert files from the edges inward, and turn on strict checks only when the code is ready for them. Follow the steps in order and you will never face a week-long rewrite or a broken main branch.

Migrate JavaScript Project to TypeScript: Pick Your Strategy First

Before touching any code, decide the order of conversion. The typescript migration strategy that works best for most projects is to start at the leaves and move toward the trunk. Convert utility functions and small helpers first, because they have few dependencies and their types are easy to describe. Then convert the modules that depend on them, and leave the entry point and the messiest files for last. Each converted file makes the next one easier, because its types are already there to guide the callers.

Set two ground rules with the team before you start. First, no big-bang pull requests that rename a hundred files at once, because those cannot be reviewed properly and they conflict with everyone else’s work. Second, the app must still build and pass its tests after every single step. If a step breaks the build, fix it before moving on. That rule sounds slow, but it is what keeps the migration from becoming a second project. The comparison of API testing tools for 2026 can help you pick a way to check your endpoints while the code underneath them changes.

Step 1: Install TypeScript and Check the Baseline

Install TypeScript as a development dependency and confirm the current JavaScript still works exactly as before. Nothing about the app should change in this step. You are only adding a tool, not changing any code.

npm install --save-dev typescript
npx tsc --version

Step 2: Add a tsconfig That Accepts Your Current Code

The compiler is driven by a tsconfig.json file, and the first version of that file should be permissive. The goal at this stage is to get the compiler running without drowning in errors. Turn on allowJs so the compiler accepts your JavaScript files, turn on checkJs only if you want it to start reporting problems in those files, and leave strict mode off for now. An allowjs tsconfig setup that starts permissive and tightens later is the standard path, and it exists precisely so teams can migrate without fixing a thousand errors on day one.

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "NodeNext",
    "allowJs": true,
    "checkJs": false,
    "strict": false,
    "outDir": "dist",
    "moduleResolution": "NodeNext",
    "esModuleInterop": true,
    "skipLibCheck": true
  },
  "include": ["src"]
}

Run npx tsc --noEmit and confirm it passes with zero errors. Keep this file in version control from the start so the whole team shares the same settings.

Step 3: Convert One Small File and Learn the Pattern

Pick the smallest, most self-contained file in the project and rename it from .js to .ts. Add types to its function parameters and return values, run the compiler, and fix whatever it reports. This first file is where the team learns the rhythm of conversion, so pick something genuinely small, a string helper or a date formatter, not a file with ten imports and a database connection.

// utils/format.ts
export function formatPrice(amount: number, currency: string): string {
  return new Intl.NumberFormat("en-US", {
    style: "currency",
    currency
  }).format(amount);
}

When the file compiles and the tests still pass, commit it and move on. The commit message should say which file changed and nothing else, so the history of the migration reads as a series of small, reviewable steps.

Step 4: Convert Files in Dependency Order

Now repeat step 3 across the codebase, always converting a file whose dependencies are already typed when possible. The reason is simple: a typed dependency gives the compiler information about what the next file receives, so it can check the next file properly. Converting in the other direction, callers before the modules they call, leaves the compiler guessing and produces weaker checks.

For each file, follow the same routine. Rename it, add types to the exported functions first, then to the internal ones, then run the compiler and the tests. When a file imports a module that is still JavaScript, the compiler treats it loosely, and that is fine. Gradual typescript adoption means living with a mixed codebase for a while, and the boundary between typed and untyped code moves a little further with every file.

Data that crosses the boundary needs the most care. When a typed file reads from an untyped module, or from an API response, the compiler cannot verify the shape, so you describe it yourself with a type and check it at runtime where it matters. If the project is an API server, the REST API starter guide with Node.js and Express is a useful reference for which shapes, request bodies and response objects, deserve types first.

Step 5: Type the Edges, Then Turn On Strict Mode

Once most files are converted, turn the strict checks on one at a time instead of all at once. The strict flag is really a bundle of smaller flags, and enabling them one by one keeps the error count manageable. Start with strictNullChecks, which catches the most common class of runtime error, reading a property of something that might be null. Fix the errors it finds, commit, and move to the next flag.

{
  "compilerOptions": {
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "noUnusedLocals": true
  }
}

Expect this step to take longer than the earlier ones, because strict mode finds real problems: places where the code assumed a value existed, where a function could return nothing, where a variable was never used. When the full strict set passes, the migration is effectively done. The code is typed, the compiler is checking it properly, and the safety TypeScript promises is actually in force.

Step 6: Wire Type Checking Into the Build and Deploy

A migration that only runs on one developer’s machine will quietly rot. Add npx tsc --noEmit to the project’s test script or CI pipeline so every pull request is checked, and make the build fail when the check fails. If the project deploys from a bundler, confirm the bundler handles .ts files and that the output directory is clean before each deploy. Then update the deployment docs so the next person to set up the project knows the build step exists.

This is also the moment to check the production setup still behaves. The walkthrough on deploying a Node.js app to a VPS with PM2 and Nginx covers the server side of that check, and the guide to building a GitHub Actions pipeline for Node.js shows where the type check fits in an automated pipeline.

Common Errors and How to Fix Them

Three errors show up in almost every migration. The first is “cannot find module,” which usually means the compiler’s module resolution does not match how the project imports files. Check the module and moduleResolution settings before touching any code. The second is “implicitly has type any,” which appears when a function parameter has no annotation and the compiler cannot infer one. Add the annotation; do not silence it with any, because each any is a hole in the safety net you are building.

The third is “possibly null or undefined,” which strict mode reports when code reads a property of a value that might not exist. The fix is a real check, an early return, or a default value, not a non-null assertion. Assertions tell the compiler to stop worrying, and they hide the exact bug strict mode exists to catch.

Frequently Asked Questions

How long does a migration take?

It depends on size and on how strict you want the end state to be. A small project of a few thousand lines can be done in a week of steady work. A large one can take months of converting a few files per pull request.

Do I need to rewrite my tests?

No. Tests written in JavaScript keep working against typed code, because the compiled output is JavaScript. You may want to convert test files later for the same type checking benefits, but it is not required and it is not a good first step.

The migration is done when the compiler runs in CI, strict mode passes, and new files are written in TypeScript by default. That is the real measure of success for a gradual migration: nothing dramatic, no heroic rewrite, just a codebase that got safer one file at a time while the product kept shipping.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.