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
- Install TypeScript and
@typespackages for dependencies. - Rename a small subset of files to
.tsand fix immediate syntax errors. - Create a
tsconfig.jsonwithallowJs: trueandcheckJs: falseinitially. - Use JSDoc annotations in remaining JavaScript files for gradual typing.
- Increase strictness flags incrementally:
noImplicitAny,strictNullChecks, thenstrict: true.
Tips
- Start with leaf modules and utilities that have few dependents.
- Use
// @ts-ignoreor// @ts-expect-errorsparingly to unblock migration without losing track of issues.
Common issues
- Third-party libraries without type definitions require
.d.tsshims or migration to typed alternatives. - Aggressive strict mode on a mixed codebase produces thousands of errors and slows adoption.