Guided Notes

Union, Intersection & Literal Types

Combining types with | and &, literal types, discriminated unions, and exhaustive checks with never.

Union types

Slide 1 / 4

A union says 'one of these types':

function formatId(id: string | number): string { return typeof id === "number" ? id.toString().padStart(6, "0") : id.toUpperCase(); }

You can only use members common to all types until you narrow (with typeof, in, instanceof or a discriminant).

← Enums & Literal Types