TypeScript with React
React TypeScript for beginners. Learn how to type props, useState generics, events, refs and children correctly, with simple examples.
On this page
1. What is TypeScript with React?
TypeScript is a type system on top of JavaScript. React files are written with the .tsx extension. The Vite react-ts template and Next.js both come with TypeScript out of the box.
You do not have to type everything. TypeScript infers a lot on its own, and your job is mostly to type props and a few edge cases.
2. Why does it matter?
In a big app it is hard to remember which component takes which props. In JavaScript a mistake shows up at runtime, often in front of the user.
| JavaScript only | With TypeScript |
|---|---|
| A wrong prop is found at runtime | A red line in the editor, immediately |
| You have to remember prop names | Autocomplete tells you everything |
| Crashes from undefined | Optional values must be checked |
| Fear of breaking things in a refactor | Rename updates every place |
3. Where is it used?
| Thing | How to type it |
|---|---|
| Props | type ButtonProps = { ... } |
| Children | React.ReactNode |
| State that can be null | useState<User | null>(null) |
| An input's onChange | React.ChangeEvent<HTMLInputElement> |
| A DOM ref | useRef<HTMLInputElement>(null) |
| Extending native props | ComponentProps<"button"> |
4. How does it work?
TypeScript works only at development and build time. Types are removed before the code reaches the browser, so there is no extra code at runtime.
5. Code example
Example 1: Props, children and optional values
import type { ReactNode } from "react";
type CardProps = {
title: string;
variant?: "primary" | "danger";
children: ReactNode;
};
export function Card({ title, variant = "primary", children }: CardProps) {
return (
<section className={variant}>
<h2>{title}</h2>
{children}
</section>
);
}Example 2: State, events and refs
import { useRef, useState, type ChangeEvent, type FormEvent } from "react";
type User = { id: number; name: string };
export function SearchForm() {
const [query, setQuery] = useState("");
const [user, setUser] = useState<User | null>(null);
const inputRef = useRef<HTMLInputElement>(null);
function handleChange(e: ChangeEvent<HTMLInputElement>) {
setQuery(e.target.value);
}
function handleSubmit(e: FormEvent<HTMLFormElement>) {
e.preventDefault();
setUser({ id: 1, name: query });
inputRef.current?.focus();
}
return (
<form onSubmit={handleSubmit}>
<input ref={inputRef} value={query} onChange={handleChange} />
<button type="submit">Search</button>
{user && <p>Found: {user.name}</p>}
</form>
);
}Example 3: Extending native button props
import type { ComponentProps } from "react";
type ButtonProps = ComponentProps<"button"> & { loading?: boolean };
export function Button({ loading = false, children, ...rest }: ButtonProps) {
return (
<button disabled={loading} {...rest}>
{loading ? "Please wait..." : children}
</button>
);
}How does the code work?
| Part | Meaning |
|---|---|
variant?: | An optional prop, with the default value in the destructuring |
"primary" | "danger" | A union type, only these two values are allowed |
useState("") | The type string is inferred automatically |
useState<User | null>(null) | Null at first, a User later |
useRef<HTMLInputElement>(null) | In React 19, useRef requires an argument |
ComponentProps<"button"> | You get onClick, type, disabled and every other native prop |
Interview tip
In React 19, ref is a normal prop, so new components do not need forwardRef. ComponentProps<"input"> includes ref too. React.FC is no longer needed, and typing the props directly is the common, clean way.
6. Common mistakes
Mistake: Using any everywhere
Why? any turns off type checking, and then TypeScript gives you no benefit.
Better: Write the right type, or use unknown and check the value.
Mistake: Not giving a generic to nullable state
Why? The type of useState(null) becomes just null, and setting an object later gives an error.
Better: Give the generic a union of User and null, as done in Example 2.
Mistake: Typing children as JSX.Element
Why? This rejects string, number, array or null children.
Better: Use ReactNode for children.
Mistake: Writing any for an event
Why? You lose both autocomplete and safety on things like e.target.value.
Better: Use ChangeEvent, FormEvent or MouseEvent with the element type.
7. What to learn before and after
| Stage | Topics |
|---|---|
| Before | Props, State with useState, Forms & Controlled Inputs |
| Alongside | useRef, Custom Hooks, Children & Composition |
| After | Testing React Apps, State Management Libraries |
Final quick revision
| Point | Detail |
|---|---|
| File | .tsx |
| Props | type Props = { ... }, with ? for optional |
| Children | ReactNode |
| State | Let it be inferred, use a generic for state that can be null |
| Events | ChangeEvent<HTMLInputElement>, FormEvent<HTMLFormElement> |
| Refs | useRef<HTMLInputElement>(null) |