Migrate a JavaScript Project to TypeScript
Convert an existing JavaScript project to TypeScript incrementally by renaming files, adding types, and enabling strict mode over time.
Prerequisites
- -Existing JavaScript project
- -Node.js 18+
- -Passing test suite (recommended)
Steps
Install TypeScript and generate tsconfig
Add TypeScript and create a permissive initial configuration that allows JavaScript files.
Start with strict: false and allowJs: true to avoid hundreds of errors on day one.
Configure tsconfig for gradual migration
Set allowJs and checkJs to enable mixed JS/TS development.
Do not enable strict: true at the start. Migrate files first, then enable strict flags incrementally.
Rename files from .js to .ts incrementally
Start with utility files and leaf modules that have no dependents.
Rename leaf files first (utilities, constants, types) then work inward to core modules and entry points.
Install type definitions for dependencies
Add @types packages for libraries that do not include their own types.
Run type checking and fix errors
Incrementally fix type errors file by file.
Use 'any' as an escape hatch for complex types initially, then refine to proper types in a follow-up pass.
Enable strict flags after all files are converted
Once all .js files are renamed to .ts, enable strict mode incrementally.
Full Script
FAQ
Discussion
Loading comments...