All Articles

Add TypeScript To An Existing React Project

Matt Pocock
Matt PocockMatt is a well-regarded TypeScript expert known for his ability to demystify complex TypeScript concepts.

Adding TypeScript to an existing React project can feel like a herculean task.

Let's break it down into a few simple steps.

Step 1: Install TypeScript

You'll first need to install TypeScript as a dev dependency:

npm install --save-dev typescript

Step 2: Add a tsconfig.json file

Next, you'll need to add a tsconfig.json file to the root of your project. This file tells TypeScript how to compile your code.

I don't recommend using tsc --init for this. It will generate a lot of unnecessary comments and hard-to-read defaults.

Instead, I recommend going to the tsconfig/bases repo to find a tsconfig.json file that matches your project.

They have different tsconfig.json files for different project types:

Step 3: Change a File To .ts (or .tsx)

Next, you'll need to change a file to .ts (or .tsx if it contains a React component).

With the vast majority of modern projects, your framework of choice will be able to just work with the new .ts file. This is because most bundlers (which frameworks use to compile your code) can handle both JavaScript and TypeScript.

If it doesn't head to that framework's TypeScript documentation to see what extra steps you need to take.

Step 4: Begin The Migration

Now that you have TypeScript installed and a tsconfig.json file, you can begin the migration.

If your project is anything larger than a few files, I recommend migrating one file at a time. This will help you avoid merge conflicts and make it easier to find bugs.

It'll also mean that any existing PRs don't need to be rewritten to include the TypeScript changes.

Your project might be in a stage where it has lots of JavaScript files AND lots of TypeScript files. This is fine as an intermediate step, and you should be able to keep shipping features while this change occurs.

I recommend Sentry's Migrating to TypeScript blog post for more information on how to do this.

Matt's signature

Share this article with your friends

`any` Considered Harmful, Except For These Cases

Discover when it's appropriate to use TypeScript's any type despite its risks. Learn about legitimate cases where any is necessary.

Matt Pocock
Matt Pocock

No, TypeScript Types Don't Exist At Runtime

Learn why TypeScript's types don't exist at runtime. Discover how TypeScript compiles down to JavaScript and how it differs from other strongly-typed languages.

Matt Pocock
Matt Pocock

Deriving vs Decoupling: When NOT To Be A TypeScript Wizard

In this book teaser, we discuss deriving vs decoupling your types: when building relationships between your types or segregating them makes sense.

Matt Pocock
Matt Pocock

NoInfer: TypeScript 5.4's New Utility Type

Learn how TypeScript's new utility type, NoInfer, can improve inference behavior by controlling where types are inferred in generic functions.

Matt Pocock
Matt Pocock