Using TypeScript with React
Learn about using TypeScript with React and how to create type-safe components.
Introduction to TypeScript and React
In this tutorial, we will learn about using TypeScript with React.
Step 1: Create a New React Project
Create a new React project using create-react-app by running the following command in your terminal:
npx create-react-app my-app --template typescript
Step 2: Define a Type-Safe Component
Create a new file called Hello.tsx and add the following code:
import * as React from 'react';
interface Props {
name: string;
}
const Hello: React.FC<Props> = ({ name }) => {
return <div>Hello, {name}!</div>;
};
This code defines a type-safe Hello component.
Step 3: Use the Component
Create a new file called App.tsx and add the following code:
import * as React from 'react';
import Hello from './Hello';
const App: React.FC = () => {
return <Hello name='Alice' />;
};
This code uses the Hello component.
Ready for more? These paid resources pick up where this lesson leaves off.
Project-based TypeScript video courses — the perfect paid next step after these free lessons.
Browse on Udemy →Hand-picked TypeScript books to master the fundamentals offline.
See on Amazon →Some links on this page are affiliate links: we may earn a commission at no extra cost to you. We only recommend tools we believe are genuinely useful.