0Pricing
TypeScript Academy · Lesson

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 allowed

All lessons in this course

  1. Primitive Types & Annotations
  2. Type Inference & any vs unknown
  3. Union & Literal Types (basic narrowing intro)
← Back to TypeScript Academy