# TypeScript in React

> How to use TypeScript to add static typing to your React components, props, state, and event handlers for safer, more predictable code.

Subject: React | Lesson 41 of 48 | 6 min read
Interactive lesson: https://codewithanimation.in/react/typescript-in-react
Previous: [React Hook Form & Zod](https://codewithanimation.in/react/react-hook-form.md) | Next: [Headless Component Libraries](https://codewithanimation.in/react/headless-ui.md)

## Adding Types to the UI

TypeScript is a superset of JavaScript that adds static types. In the context of React, it allows you to define exactly what "shape" your data should take.

Instead of discovering a bug at runtime because you accidentally passed a number instead of a string to a component, TypeScript catches the error in your editor before you even save the file.

## The End of PropTypes

Historically, React developers used a library called `PropTypes` for runtime type checking. TypeScript completely replaces `PropTypes`.

TypeScript provides zero-runtime-cost compile-time checking, vastly superior editor auto-completion (IntelliSense), and makes refactoring large codebases infinitely safer.

## Typing the Core Concepts

There are four main areas you need to type in a React app:

1. **Props:** Use an `interface` or `type` to define what props a component accepts: `function Avatar({ url, size }: AvatarProps)`

2. **State:** Often inferred, but for complex or initially null state, provide a generic: `useState<User | null>(null)`

3. **Events:** Use React's built-in event types: `(e: React.ChangeEvent<HTMLInputElement>) => void`

4. **Refs:** Provide the specific DOM element type: `useRef<HTMLDivElement>(null)`

## Common mistake: The Children Prop

Typing `children` used to be tricky. The modern standard is to use React's built-in `ReactNode` type, which covers anything that React can render (strings, numbers, elements, arrays of elements, or null).

`interface CardProps { children: React.ReactNode; title: string; }`

## Tip: Don't use React.FC

In older tutorials, you will see components typed as `const Button: React.FC<ButtonProps> = ...`.

This pattern is now officially discouraged by the React core team. Just type the function arguments directly: `function Button({ text }: ButtonProps)`.

## Try it: JS vs TS Components

Compare a standard JavaScript React component with its TypeScript equivalent. Notice how TypeScript explicitly defines the shape of props, state, events, and refs, providing ultimate editor safety.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/react/typescript-in-react

## Check yourself

### Question 1

What is the recommended way to type the props of a React component?

- A. Using the `PropTypes` library.
- B. Defining an `interface` or `type` alias and applying it to the function's parameter object.
- C. Using `React.FC<Props>` on the function signature.

Correct answer: B

Defining an interface and typing the destructured props argument directly is the modern standard, replacing both `PropTypes` and the deprecated `React.FC` pattern.

### Question 2

When do you explicitly need to provide a type generic to `useState` (e.g., `useState<number>()`)?

- A. Always. TypeScript cannot infer the type of `useState`.
- B. When the initial state is `null` or an empty array `[]`, but it will later hold a specific type of data.
- C. Only when storing objects.

Correct answer: B

If you do `useState(0)`, TypeScript correctly infers `number`. But if you do `useState(null)`, it infers `null`, and you won't be able to store a User object in it later unless you explicitly declare `useState<User | null>(null)`.

### Question 3

What is the correct type to use for a component's `children` prop?

- A. `JSX.Element`
- B. `React.ReactNode`
- C. `any`

Correct answer: B

`ReactNode` is a union type that includes everything React can validly render: elements, strings, numbers, booleans, null, undefined, and arrays of these.

- Why not A: This excludes strings and arrays.

## Remember this

- TypeScript eliminates runtime type errors by catching them in your editor.
- Type your props using an `interface` or `type`.
- Use `React.ReactNode` for the `children` prop.
- Pass a generic to `useState<Type>()` if the initial value doesn't provide enough information (like `null` or `[]`).
- Use React's built-in event types (like `React.MouseEvent`) for event handlers.
