How to migrate a JavaScript project to TypeScript

· Category: TypeScript

Short answer

Rename files from .js to .ts, add a tsconfig.json, and run the TypeScript compiler. Start with loose settings and tighten them over time. For understanding type system concepts, see python type hints. For writing tests during migration, see python testing with pytest.

Steps

  1. Install TypeScript: npm install -D typescript
  2. Generate a config: npx tsc --init
  3. Rename entry files from .js to .ts
  4. Allow JS in tsconfig: "allowJs": true
  5. Fix compiler errors incrementally and enable strict mode later

Tips

  • Migrate leaf modules first before core files
  • Use @ts-check in JS files before converting them
  • For performance considerations during migration, review how to improve core web vitals