You can use Create React App (CRA) to quickly set up a new React project with TypeScript support. Open your terminal and run the following command:
npx create-react-app my - react - ts - app --template typescript
cd my - react - ts - app
npm start
This will create a new React project with TypeScript support and start the development server. You can now open your browser and navigate to http://localhost:3000
to see your app running.
TypeScript allows you to define types for variables, functions, and objects. Here’s a simple example of defining a variable with a type:
let message: string = 'Hello, TypeScript!';
In this example, the message
variable is of type string
. If you try to assign a non - string value to it, TypeScript will raise a compilation error.
In TypeScript, React components can be written as either functional components or class components. Here’s an example of a functional component:
import React from 'react';
const HelloWorld: React.FC = () => {
return <div>Hello, World!</div>;
};
export default HelloWorld;
Here, React.FC
(Function Component) is a type that represents a React functional component.
When working with React components, you often need to define types for props and state.
import React from 'react';
interface Props {
name: string;
age: number;
}
const Person: React.FC<Props> = ({ name, age }) => {
return (
<div>
<p>Name: {name}</p>
<p>Age: {age}</p>
</div>
);
};
export default Person;
In this example, we define an interface
named Props
to specify the types of the props that the Person
component expects.
import React, { useState } from 'react';
interface State {
count: number;
}
const Counter: React.FC = () => {
const [state, setState] = useState<State>({ count: 0 });
const increment = () => {
setState(prevState => ({ ...prevState, count: prevState.count + 1 }));
};
return (
<div>
<p>Count: {state.count}</p>
<button onClick={increment}>Increment</button>
</div>
);
};
export default Counter;
Here, we define an interface
for the state and use useState
with the state type specified.
When handling events in React with TypeScript, you need to specify the event type. For example, for a button click event:
import React from 'react';
const ClickableButton: React.FC = () => {
const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
console.log('Button clicked!');
};
return <button onClick={handleClick}>Click me</button>;
};
export default ClickableButton;
Interfaces are a great way to define the shape of props and state in React components. They make your code more readable and maintainable. For example, if you have a complex component with many props, you can use an interface to clearly define their types:
interface ComplexProps {
id: number;
title: string;
description: string;
isActive: boolean;
}
const ComplexComponent: React.FC<ComplexProps> = ({ id, title, description, isActive }) => {
// Component logic here
return <div>{title}</div>;
};
Type assertion is used when you know the type of a value better than TypeScript does. However, it should be used sparingly as it can bypass TypeScript’s type checking. Here’s an example:
const element = document.getElementById('my - element') as HTMLDivElement;
In this example, we assert that the element retrieved by getElementById
is of type HTMLDivElement
.
Union types allow a variable to have one of several types. While they can be useful in some cases, overusing them can make your code hard to understand and maintain. For example, instead of using a union type like string | number
for a prop, try to find a more specific type or refactor your code.
TypeScript has powerful type inference capabilities. You don’t always need to explicitly define types for every variable. For example:
const numbers = [1, 2, 3]; // TypeScript infers the type as number[]
Combining TypeScript with React can bring many benefits to your development process, such as improved code quality, better autocompletion, and early error detection. By understanding the fundamental concepts, usage methods, common practices, and best practices, you can start building robust and maintainable React applications with TypeScript. As you gain more experience, you’ll be able to leverage the full power of both technologies to create high - quality user interfaces.