Menu

Migrating JavaScript to TypeScript

Lesson 5: Migrating JavaScript to TypeScript

How do you migrate without stopping work?

Migrate gradually, so the app keeps running at every step.

Step Action Result
1. Add TypeScript Install typescript, add tsconfig.json with allowJs: true, noEmit: true JS and TS files live together
2. Check JavaScript Enable checkJs (or add // @ts-check to chosen files) Find bugs before renaming anything
3. Add JSDoc types /** @param {number} price */ in key JS files Types without changing file extensions
4. Convert files Rename .js to .ts starting with leaf modules (utilities, types) Real types, one file at a time
5. Tighten Turn on strict options one by one if they were disabled Full safety
6. Remove escape hatches Replace any with real types or unknown Clean codebase

Example

// tsconfig.json during migration
{
  "compilerOptions": {
    "allowJs": true,
    "checkJs": true,
    "noEmit": true,
    "strict": false,
    "noImplicitAny": false
  },
  "include": ["src"]
}
// src/price.js – typed with JSDoc before conversion
// @ts-check

/**
 * @param {number} paise
 * @param {number} [gstPercent=5]
 * @returns {number}
 */
export function withGst(paise, gstPercent = 5) {
  return Math.round(paise * (1 + gstPercent / 100));
}
// src/price.ts – after conversion
export function withGst(paise: number, gstPercent = 5): number {
  return Math.round(paise * (1 + gstPercent / 100));
}

How it works

  • Since TypeScript 6.0 makes strict the default, a migrating project may need to set "strict": false at first, then turn individual checks back on.
  • Convert from the leaves inward: utilities and shared types first, then modules that import them.
  • Use // @ts-expect-error with a short comment for known issues; unlike @ts-ignore, it reports an error once the issue is fixed, so it does not linger.
  • Track progress: count remaining .js files and any uses, and lower them each sprint.

Common mistakes

Mistake Effect Fix
Converting everything in one huge pull request Hard to review, blocks the team Convert a few files per pull request
Typing everything as any to get it compiling Migration with no benefit Use unknown, or leave a file as JS until it can be typed
Never turning strict back on Most bugs remain possible Plan dates to enable each strict check

Key takeaways

  • Migrate gradually: allow JS, check JS, add JSDoc, convert leaf files, then tighten.
  • Every step should leave the app working.

Try it: Take a small JavaScript project, add a tsconfig.json with allowJs and checkJs, fix the reported errors, then convert two files to .ts.


TypeScript Tutorial

J

Jebasta





Get in Touch For Details