ts-migrate: A Tool for Migrating to TypeScript at Scale

Learn about how we used codemods to accelerate migration from JavaScript to TypeScript at Airbnb.

Sergii Rudenko
12 min readintermediate
--
View Original

Overview

The article discusses 'ts-migrate', a tool developed at Airbnb to facilitate the migration of large JavaScript codebases to TypeScript. It outlines various migration strategies, the development of the tool, and the steps involved in the migration process, highlighting the use of codemods to automate repetitive tasks.

What You'll Learn

1

How to implement a hybrid migration strategy from JavaScript to TypeScript

2

Why using codemods can accelerate the migration process

3

How to create and utilize tsconfig.json for TypeScript projects

4

When to apply React-specific plugins during migration

Prerequisites & Requirements

  • Basic understanding of TypeScript and JavaScript
  • Familiarity with codemod tools like jscodeshift(optional)

Key Questions Answered

What are the advantages of using an all-in migration strategy?
An all-in migration strategy ensures consistency across the project, as all files are converted at once. This approach simplifies the process of fixing types since engineers do not have to remember which files are TypeScript and which are JavaScript, leading to a more manageable migration process.
How does ts-migrate automate the migration process?
ts-migrate automates the migration process through the use of codemods, which are scripts that modify code based on specific rules. By running these codemods, developers can quickly convert JavaScript files to TypeScript, reducing manual effort and errors.
What steps are involved in migrating a project from JavaScript to TypeScript?
The migration process involves creating a tsconfig.json file, changing file extensions from .js/.jsx to .ts/.tsx, and running codemods to automate code transformations. Each step is tracked with Git to ensure clarity and manageability.
What is the purpose of the ts-migrate-server?
The ts-migrate-server coordinates the migration process by parsing the tsconfig.json file, creating .ts source files, and sending each file to the TypeScript language server for diagnostics. This ensures that all necessary modifications are made efficiently.

Key Statistics & Figures

Percentage of frontend monorepo converted to TypeScript
86%
As of the article's writing, Airbnb has converted approximately 86% of its 6 million line frontend monorepo to TypeScript.
Number of lines of code converted in one day using codemods
50,000 lines
The use of codemods allowed Airbnb to convert projects with more than 50,000 lines of code from JavaScript to TypeScript in just one day.

Technologies & Tools

Some links below are affiliate links. We may earn a commission if you make a purchase.

Key Actionable Insights

1
Adopting the all-in migration strategy can significantly streamline the transition to TypeScript.
This approach ensures that all files are consistent and reduces the cognitive load on developers, making it easier to manage type errors across the codebase.
2
Utilizing codemods can save time and reduce manual errors during migration.
By automating repetitive tasks, codemods allow engineers to focus on improving type definitions rather than on manual file-by-file conversion.
3
Regularly updating the tsconfig.json file is crucial for maintaining project consistency.
A well-configured tsconfig.json ensures that all TypeScript features are utilized correctly, leading to fewer runtime errors and better code quality.

Common Pitfalls

1
Failing to properly configure tsconfig.json can lead to inconsistent behavior across the codebase.
Without a well-defined tsconfig.json, developers may encounter unexpected type errors or runtime issues, making debugging more challenging.
2
Relying too heavily on @ts-ignore comments can mask underlying issues in the code.
While @ts-ignore can temporarily suppress errors, overusing it can lead to a codebase filled with hidden problems that may complicate future maintenance.

Related Concepts

Typescript Migration Strategies
Codemods And Their Applications
Best Practices For Typescript Configuration