Skip to content
EcosystemChapter 36 / 37Intermediate4 min read

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 onlyWith TypeScript
A wrong prop is found at runtimeA red line in the editor, immediately
You have to remember prop namesAutocomplete tells you everything
Crashes from undefinedOptional values must be checked
Fear of breaking things in a refactorRename updates every place

3. Where is it used?

ThingHow to type it
Propstype ButtonProps = { ... }
ChildrenReact.ReactNode
State that can be nulluseState<User | null>(null)
An input's onChangeReact.ChangeEvent<HTMLInputElement>
A DOM refuseRef<HTMLInputElement>(null)
Extending native propsComponentProps<"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.

The journey of a type check
Write the component in .tsx
Define the types for props and state
The editor and tsc catch mismatches
Types are stripped at build time
Plain JavaScript runs in the browser

5. Code example

Example 1: Props, children and optional values

tsx
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

tsx
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

tsx
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?

PartMeaning
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

Final quick revision

PointDetail
File.tsx
Propstype Props = { ... }, with ? for optional
ChildrenReactNode
StateLet it be inferred, use a generic for state that can be null
EventsChangeEvent<HTMLInputElement>, FormEvent<HTMLFormElement>
RefsuseRef<HTMLInputElement>(null)