Types and narrowing — TypeScript · Foundations

Describe data with types precise enough to rule out wrong values, then turn a value of unknown shape, such as a request body, into one TypeScript trusts, one check at a time.

What you will learn

Read the theory for Types and narrowing

All TypeScript lessons

All Foundations courses

loading types…

What you'll learn

  • Write union and literal types that allow exactly the values that make sense
  • Narrow unknown with typeof, equality, Array.isArray and in, and write a type predicate
  • Model a result that is one of two shapes as a discriminated union, and narrow on its tag