How to migrate JavaScript to TypeScript

· Category: TypeScript

Short answer

Rename files to .ts or .tsx, enable loose compiler options, add type declarations for external libraries, and tighten strictness over time.

Steps

  1. Install TypeScript and @types packages for dependencies.
  2. Rename a small subset of files to .ts and fix immediate syntax errors.
  3. Create a tsconfig.json with allowJs: true and checkJs: false initially.
  4. Use JSDoc annotations in remaining JavaScript files for gradual typing.
  5. Increase strictness flags incrementally: noImplicitAny, strictNullChecks, then strict: true.

Tips

  • Start with leaf modules and utilities that have few dependents.
  • Use // @ts-ignore or // @ts-expect-error sparingly to unblock migration without losing track of issues.

Common issues

  • Third-party libraries without type definitions require .d.ts shims or migration to typed alternatives.
  • Aggressive strict mode on a mixed codebase produces thousands of errors and slows adoption.