Chapter 23: TypeScript with React

TypeScript with React like we’re sitting together in a calm coding session in Hyderabad. No rush, step-by-step, whiteboard style — with real patterns people use in early 2026.

We’ll cover:

  • Why TS + React is now the default for most serious frontend work
  • Modern project setup (Vite vs Next.js — what most people choose in 2026)
  • Typing components, props, state, events
  • Hooks with full TypeScript (useState, useEffect, custom hooks)
  • Common patterns & gotchas in 2026
  • Real small examples you can copy-paste

Ready? Let’s go.

1. Why TypeScript + React in 2026?

Quick reality check (from State of JS / State of React surveys 2025):

  • ~78–85% of professional React developers use TypeScript
  • Almost every new job posting that mentions React also asks for TypeScript
  • Big reasons:
    • Catches ~70–90% of bugs before runtime (props mismatch, wrong hook usage, null/undefined crashes)
    • Incredible autocompletion & refactoring in VS Code
    • Self-documenting components (hover over prop → see exactly what it expects)
    • Scales beautifully in teams of 5–500 people
    • Works perfectly with modern tools (Vite, Next.js App Router, TanStack Query, Zustand, shadcn/ui, etc.)

2. Recommended setup in February 2026

Two dominant paths:

A. Vite + React + TypeScript (most popular for SPA / dashboard / internal tools / component libraries)

Bash

tsconfig.json (Vite generates very good defaults — usually just add paths if needed)

JSON

B. Next.js 14/15 + App Router + TypeScript (best for SEO, server components, full-stack apps)

Bash

Next.js auto-generates excellent tsconfig.json — rarely need to touch it.

Which to choose in 2026?

  • SPA / admin panel / dashboard / design system → Vite + React-TS
  • Marketing site / e-commerce / blog / public-facing app with SSR/SSG → Next.js + TypeScript

3. Typing Functional Components (the modern way)

In 2026 almost everyone uses functional components + hooks (class components are legacy).

Two main styles — both are correct:

Style 1: Explicit interface + FC (still very common)

tsx

Style 2: No FC — just function with props type (gaining popularity — cleaner children typing)

tsx

Tip 2026: Many teams prefer Style 2 because React.FC auto-adds children?: ReactNode even when you don’t want it.

4. Typing Hooks — the bread & butter

useState

tsx

useEffect

tsx

useReducer (with discriminated unions — very powerful)

tsx

Custom Hook example (very common pattern)

tsx

5. Typing Events & Forms

tsx

6. Quick 2026 best-practice cheat sheet

Thing Recommended way 2026 Why / Note
Component type Plain function + props type (avoid React.FC) Better children control
State init null useState<User null>(null)
Generic components function List<T>({ items }: { items: T[] }) Reusable table / card list
Event handlers React.ChangeEvent<HTMLInputElement> Full safety
useEffect deps exhaustive-deps ESLint rule on Catches missing deps
Custom hooks Return tuple + as const Preserve literal types
Children children?: React.ReactNode Or more specific ReactElement

Your mini homework for today

  1. Create Vite + React-TS project (npm create vite@latest)
  2. Make a <UserCard user: { name: string; age?: number } /> component
  3. Add useState + useEffect to fetch mock users
  4. Intentionally pass wrong prop type → watch the red squiggle

Any part you want to zoom into deeper?

  • Generic components with children
  • Typing React Context
  • Next.js App Router + Server Components + TS
  • useReducer + Zustand / Redux Toolkit patterns
  • Common ESLint + Prettier + TS setup

Just tell me — we’ll continue from exactly there! 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *