Chapter 40: TypeScript Exercises

TypeScript Exercises guide.

I prepared it as if we are in a small group workshop right now (Feb 2026). We go from beginner → intermediate → advanced → real-world-ish patterns.

Each section has:

  • short goal explanation
  • the exercise itself
  • difficulty rating
  • hints (if you get stuck)
  • example solution (scroll down only after trying!)

You can solve them directly in https://www.typescriptlang.org/play (recommended) or in your own project.

Level 1 – Warm-up (very beginner friendly)

Exercise 1.1 – Basic annotations & inference Difficulty: ★☆☆☆☆

TypeScript

Exercise 1.2 – Literal types + union Difficulty: ★★☆☆☆

Create type Size that only allows these exact string values: “xs” | “sm” | “md” | “lg” | “xl” | “2xl”

Then create function getPadding(size: Size) that returns different padding values:

TypeScript

Bonus: make sure TypeScript autocompletes the possible values when you write getPadding(“…

Level 2 – Functions, objects, arrays

Exercise 2.1 – Function overloads or generics Difficulty: ★★☆☆☆

Write function firstElement that:

  • accepts array of anything
  • returns first element or undefined if array is empty
  • preserves the exact element type
TypeScript

Exercise 2.2 – Deep partial type Difficulty: ★★★☆☆

Create type DeepPartial<T> so that this works:

TypeScript

Level 3 – Real-world patterns

Exercise 3.1 – Discriminated union + exhaustive check Difficulty: ★★★☆☆

TypeScript

Exercise 3.2 – Branded / nominal types for IDs Difficulty: ★★★★☆

Create two branded types so that these lines give error:

TypeScript

Exercise 3.3 – infer + conditional types Difficulty: ★★★★☆

Create type UnwrapPromise<T> that recursively unwraps nested promises:

TypeScript

Level 4 – React + modern ecosystem patterns

Exercise 4.1 – Typed custom hook

Create useLocalStorage hook with full type safety:

tsx

Exercise 4.2 – Generic table component props

tsx

Bonus – Pick one harder challenge

Challenge A – Path type extractor Create type Paths<T> that produces union of all possible deep key paths:

TypeScript

Challenge B – Type-safe route builder Create function route that builds URL with parameters safely:

TypeScript

How to practice effectively

  1. Do 2–4 exercises per day (not more)
  2. Always try first without looking at hints/solution
  3. After you finish → compare with my solution → ask yourself: „Which version is easier to read / refactor / extend?“
  4. Put your solutions into a GitHub repo called ts-exercises
  5. After ~30 exercises → start using these patterns in your real project

Which level / exercise would you like to start with right now? Or do you want me to pick 5 exercises that match your current skill best?

Just tell me your preference 😄

You may also like...

Leave a Reply

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