AI Hints
Home›TypeScript Tutorial›Testing, Migrating, Mistakes
TypeScript · Lesson 95 of 120

Migrating JavaScript

Step by step.

⏱ 2 min read▶ Video at 1:14:19🎓 Free
Watch this lesson in the TypeScript courseWatch on YouTube ↗Full TypeScript course

Most teams don't start with TypeScript. They move an existing JavaScript project over, one file at a time. TypeScript is designed for exactly that.

Migrating JavaScript
From the video: Migrating JavaScript
  • Existing JavaScript
  • One file at a time
  • Designed for it

A safe migration

First, add a tsconfig that allows JavaScript. Then rename one file to dot TS. Fix its errors. Repeat, file by file. Finally, turn on strict, and fix what remains.

  1. allowJs
  2. Rename one file
  3. Fix errors
  4. Repeat
  5. strict
A safe migration
From the video: A safe migration

Before and after

The JavaScript version accepts anything, and can return the wrong thing. The TypeScript version names its inputs and output, so every caller is checked.

JavaScript

  • function total(items)No typesBugs found late

TypeScript

  • function total(items: Item[]): numberTypedBugs found early

Summary

Migrate one file at a time, then switch on strict. Next, common mistakes.

Key takeaways
  • allowJs
  • One file
  • Then strict
Next up: Common Mistakes — and fixes