Union and intersection vocabulary

  • A | B (union) — value is either A or B; use typeof/instanceof/in to narrow before accessing specific members
  • Discriminated union — shared literal property (kind/type/tag) uniquely identifies each member; switch on it to narrow
  • A & B (intersection) — value must satisfy both A and B; has all properties of both types
  • never — bottom type; no value exists; use in default case for exhaustiveness checking
  • param is Type (type predicate) — annotates a function to tell TypeScript: "if this returns true, narrow param to Type"

Question 0 of 5

What does string | number mean as a TypeScript type?

function formatId(id: string | number): string { if (typeof id === "number") { return id.toString().padStart(6, "0"); } return id; } formatId(42); // "000042" formatId("USR-7"); // "USR-7"