Chapter 42: TypeScript Exercises

TypeScript Exercises guide — written exactly as if we are sitting together in a small coding workshop right now (February 2026).

I prepared progressive exercises that start very easy and become gradually more realistic / production-like. Each block contains:

  • short goal explanation
  • the exercise task
  • difficulty rating
  • hints (only read if stuck)
  • example solution (scroll down only after you tried!)

You can solve everything directly in the official TypeScript Playground: 👉 https://www.typescriptlang.org/play

Level 1 – Warm-up (very beginner friendly)

Exercise 1.1 – Fix inference & add basic types Difficulty: ★☆☆☆☆

TypeScript

Exercise 1.2 – Literal union type + function Difficulty: ★★☆☆☆

Create type Size that only allows these exact values:

“xs” | “sm” | “md” | “lg” | “xl” | “2xl”

Then write function getPadding(size: Size) that returns:

text

Make sure VS Code autocompletes the possible values when you type getPadding(“…

Level 2 – Functions, objects, unions, generics

Exercise 2.1 – Generic first/last element Difficulty: ★★☆☆☆

Write two generic functions:

TypeScript

So that these lines have correct types:

TypeScript

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

Create type DeepPartial<T> so this is valid:

TypeScript

Level 3 – Discriminated unions, narrowing, exhaustiveness

Exercise 3.1 – State machine with exhaustive check Difficulty: ★★★☆☆

TypeScript

Exercise 3.2 – Branded types for safety Difficulty: ★★★★☆

Make sure these two calls are not accidentally swapped:

TypeScript

Level 4 – Real-world-ish exercises

Exercise 4.1 – useLocalStorage hook with strong types

tsx

Exercise 4.2 – Generic table component props

tsx

Bonus – Pick one harder challenge

Challenge A – Deep key paths union

TypeScript

Challenge B – Type-safe route builder

TypeScript

How to get the most out of these exercises

  1. Do 2–4 exercises per day — not more (quality > quantity)
  2. Always try first without looking at hints/solution
  3. After finishing → compare your version with the example 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–40 exercises → start applying these patterns in your real project

Which level / exercise would you like to start with right now? Or tell me your current skill level (beginner / intermediate / advanced) and I can pick 5 exercises that match best.

Ready when you are 😄

You may also like...

Leave a Reply

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