7 Understanding static types

This chapter covers

  • Restricting the range of types that can be as signed to variables or used in operations
  • Relying on the compiler to infer types
  • Using any, never, and unknown types to broaden or restrict the range of values
  • Creating type unions that combine multiple types
  • Using type assertions and type guards to narrow types
  • Controlling how the JavaScript null and undefined values can be used

In this chapter, I introduce the key TypeScript features for working with data types. The features I describe in this chapter are the foundations for working with TypeScript, and they are the building blocks for the advanced features described in later chapters.

I start by showing how TypeScript’s types differ from pure JavaScript’s ...

Get Essential TypeScript 5, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.