Command Palette

Search for a command to run...

Hectal
Phase 12Intermediate13 of 28 in React

TypeScript with React

Type props, children, state, refs, reducers, events, context and custom hooks, then build generic and polymorphic components the compiler keeps honest.

TypeScript is JavaScript plus types: little labels that say what kind of value each variable, prop and function expects. A program called the type checker reads those labels before your code runs and points at mistakes, such as a misspelled prop, a number where a string belongs, or a value that might be null. Most professional React code is written in TypeScript, and most React job interviews assume you can read and write it.

In this phase you'll set up a React + TypeScript project with Vite and learn to read the compiler's messages. Then you'll type everything a component touches: props and children, props borrowed from real HTML elements, useState, useRef and useReducer (with action types the compiler checks for you), events and handler props, context with a safe guard hook, and custom hooks. The last topic builds the components libraries are made of: generic lists and selects, a polymorphic as prop, props that change shape with a kind field, and the satisfies operator.

One rule runs through every topic: types are checked while you write, and erased when the code runs. The live examples here are real TSX, but the preview strips the types without checking them (like Vite does), so every type error is shown next to the example with the exact message the real tsc compiler prints. You'll learn what types can promise, and the places (like data from a server) where they can't.

0/6 · 0%
6 topics ~3 h 20 code blocks & diagrams
Start with the first topic
1
12.1

Why TypeScript with React

TypeScript adds types to JavaScript so a checker can catch wrong, missing and misspelled props, and values that might be null, while you type; React projects use .tsx files, the @types/react package and strict mode, and Vite strips the types without checking them.

28 min 1 diagram 3 code practice

2
12.2

Typing Props and Children

Describe a component's props with a type or interface: optional props with ?, unions of string literals for variants, ReactNode for anything renderable, and ComponentProps<"button"> to accept every native prop of an HTML element.

27 min 3 code practice

3
12.3

Typing State, Refs and Reducers

Let useState infer simple types, but give it a type argument when the start value is null or an empty array; type DOM refs as useRef<HTMLInputElement>(null); and type reducer actions as a discriminated union so a switch with a never check proves every action is handled.

29 min 4 code practice

4
12.4

Typing Events and Handlers

Inline handlers get their event type for free; separate handler functions use React's event types such as ChangeEvent<HTMLInputElement>, MouseEvent<HTMLButtonElement> and FormEvent<HTMLFormElement>, and event.currentTarget is the typed element the handler is attached to.

26 min 3 code practice

5
12.5

Typing Context and Custom Hooks

Create context as createContext<Value | null>(null) and read it through a guard hook that throws outside the provider, so callers never see null; return tuples from custom hooks as const, and make hooks generic when the caller decides the type.

26 min 3 code practice

6
12.6

Generic and Polymorphic Components

Generic components (List<T>, Select<T>) keep the caller's item type all the way into callbacks; a polymorphic as prop lets one component render any element with that element's props; discriminated union props make invalid prop combinations impossible; and satisfies checks a value against a type without losing its exact shape.

30 min 3 code practice

Practise this phase

Hands-on exercises: write the code, see your page live, and press Check to have it tested automatically.