How to Add TypeScript to a React Project
- Step 1: Install TypeScript and Necessary Types
- Step 2: Create a tsconfig.json File
- Step 3: Rename Files to .tsx
- Step 4: Update Your Code with Type Annotations
- Conclusion
- FAQ

Adding TypeScript to a React project can significantly enhance your development experience by providing strong typing, improved code quality, and better tooling support. If you’re familiar with JavaScript but are looking to incorporate TypeScript into your workflow, you’re in the right place. In this guide, we’ll walk you through the steps to seamlessly integrate TypeScript into your existing React project. Whether you’re starting fresh or adding TypeScript to an ongoing project, we’ll cover everything you need to know. So, roll up your sleeves, and let’s dive in!
Step 1: Install TypeScript and Necessary Types
The first step in adding TypeScript to your React project is to install the TypeScript package and the necessary types for React. You can do this easily using npm or yarn. To begin, navigate to your project directory in the terminal and run the following commands:
npm install --save-dev typescript @types/react @types/react-dom
or
yarn add --dev typescript @types/react @types/react-dom
This command installs TypeScript itself along with the type definitions for React and ReactDOM. These type definitions provide TypeScript with the information it needs to understand the types used in React, ensuring that you get the benefits of type-checking and autocomplete features in your IDE.
After running the command, you’ll see TypeScript and the type definitions added to your devDependencies
in the package.json
file. This is a crucial step as it sets up the foundation for using TypeScript in your React components.
Output:
+ typescript@latest
+ @types/react@latest
+ @types/react-dom@latest
Step 2: Create a tsconfig.json File
Next, you need to create a tsconfig.json
file in the root of your project. This file is essential as it tells TypeScript how to compile your code. You can create this file manually or use the TypeScript CLI to generate a default configuration.
To create it manually, run the following command:
npx tsc --init
This command initializes a new tsconfig.json
file with default settings. You can customize these settings based on your project requirements. Here’s a basic example of what your tsconfig.json
might look like:
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "commonjs",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "react-jsx"
},
"include": ["src"]
}
This configuration specifies various compiler options, including the target ECMAScript version and the libraries to include. The strict
option enables strict type-checking options, which is highly recommended when working with TypeScript.
Output:
tsconfig.json created with default settings
Step 3: Rename Files to .tsx
Now that TypeScript is set up, the next step is to rename your existing React component files from .js
or .jsx
to .ts
or .tsx
. The .tsx
extension is specifically for files that contain JSX syntax. This change allows TypeScript to recognize these files and apply type-checking.
For example, if you have a file named App.js
, rename it to App.tsx
. You can do this manually or use a command in your terminal. Here’s how you might do it:
mv src/App.js src/App.tsx
After renaming your files, you may need to address any type errors that TypeScript reports. This step is crucial as it helps you adopt TypeScript’s type system effectively. You might find that adding types to your props and state can prevent potential bugs and improve your code quality.
Output:
App.js renamed to App.tsx
Step 4: Update Your Code with Type Annotations
With TypeScript now integrated into your project, it’s time to update your code with type annotations. This step enhances code clarity and helps you catch errors early. You can start by defining types for your component props and state.
Here’s a simple example of a functional component with props:
import React from 'react';
interface GreetingProps {
name: string;
}
const Greeting: React.FC<GreetingProps> = ({ name }) => {
return <h1>Hello, {name}!</h1>;
};
export default Greeting;
In this example, we define a GreetingProps
interface that specifies that the name
prop must be a string. By using React.FC<GreetingProps>
, we ensure that our component adheres to the prop type defined in the interface. This approach not only improves code readability but also provides better tooling support in your IDE.
Output:
Component updated with type annotations
Conclusion
Adding TypeScript to your React project is a straightforward process that can greatly enhance your development experience. By following these steps—installing TypeScript and type definitions, creating a tsconfig.json
file, renaming your files, and updating your code with type annotations—you can leverage TypeScript’s powerful type system to improve code quality and maintainability. As you become more familiar with TypeScript, you’ll find that it helps you catch errors early and enhances your overall productivity. So, go ahead and take the plunge into TypeScript; your future self will thank you!
FAQ
-
What are the benefits of using TypeScript with React?
TypeScript provides strong typing, better tooling support, and helps catch errors during development, resulting in more maintainable code. -
Can I use TypeScript with existing JavaScript React projects?
Yes, you can gradually migrate your existing JavaScript React project to TypeScript by renaming files and adding type annotations. -
Do I need to learn TypeScript before using it with React?
While familiarity with TypeScript is beneficial, you can start using it with React and learn as you go. Many resources are available to help you along the way. -
What is the difference between .ts and .tsx files?
Files with a .ts extension are for TypeScript code, while .tsx files are specifically for TypeScript files that contain JSX syntax. -
Are there any performance impacts when using TypeScript with React?
Generally, TypeScript does not have a significant performance impact on your application. The benefits of type-checking and improved code quality often outweigh any minor performance considerations.
Rana is a computer science graduate passionate about helping people to build and diagnose scalable web application problems and problems developers face across the full-stack.
LinkedIn