Union & Literal Types (basic narrowing intro)
Combine types with unions, use literal types for exact values, and apply basic narrowing.
Intro
Goal: Learn how to combine multiple types with union and restrict values with literal types. These help create safer APIs.
Union basics
Union types use | to allow one of several types. Here: string or number.
let value: string | number;
value = "hello";
value = 42;
// value = true;
// Error, not allowedAll lessons in this course
- Primitive Types & Annotations
- Type Inference & any vs unknown
- Union & Literal Types (basic narrowing intro)