0Pricing
TypeScript Academy · Lesson

Union Types: A or B

Use | to allow multiple type possibilities.

Welcome

Union types allow a value to be one of several types. They are written with the `|` operator and are fundamental to modeling real-world data.

Basic Union Syntax

Combine two or more types with `|`. A variable with a union type can hold any one of the listed types.
let id: string | number;
id = 'abc123'; // OK
id = 42;       // OK
// id = true;  // Error

All lessons in this course

  1. Union Types: A or B
  2. Intersection Types: A and B
  3. Discriminated Unions for Safe Pattern Matching
  4. Practical Patterns with Union and Intersection
← Back to TypeScript Academy