TypeScript Academy · Lesson

Union & Literal Types (basic narrowing intro)

Combine types with unions, use literal types for exact values, and apply basic narrowing.

Lesson 3 of 38 steps

Union & Literal Types (basic narrowing intro) is a free TypeScript Academy lesson on CoddyKit. This is lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The TypeScript Academy course includes 3 lessons in total.

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

Literal types

Literal types restrict values to exact options. Common for configs and enums.

let direction: "left" | "right";
direction = "left";

// direction = "up";
 // Error

Narrowing basics

Narrowing checks the runtime type (e.g., typeof) to safely access members.

function printId(id: string | number) {
  if (typeof id === "string") {
    console.log(id.toUpperCase());
  } else {
    console.log(id.toFixed(2));
  }
}

printId("abc");
printId(123.456);

Discriminated unions

Discriminated unions combine literal tags with objects. Narrowing on kind makes TypeScript smart.

type Shape =
  | { kind: "circle"; radius: number }
  | { kind: "square"; size: number };

function area(shape: Shape): number {
  if (shape.kind === "circle") {
    return Math.PI * shape.radius ** 2;
  } else {
    return shape.size * shape.size;
  }
}

console.log(area({ kind: "circle", radius: 2 }));
console.log(area({ kind: "square", size: 3 }));

Why unions & literals

Why use unions & literals? They make invalid states impossible, guide autocomplete, and let the compiler catch mistakes earlier.

Union check

Quick check: What is a union type?

Recap

Recap: Use | for unions, literal types for exact values, and narrowing to branch safely. These tools make TS expressive and safe.

Free to start

Learn TypeScript with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
101
Lessons
352

Frequently Asked Questions

Is the “Union & Literal Types (basic narrowing intro)” lesson free?

Yes — the full text of “Union & Literal Types (basic narrowing intro)” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO. The TypeScript Academy course includes 3 lessons in total.

What will I learn in “Union & Literal Types (basic narrowing intro)”?

Combine types with unions, use literal types for exact values, and apply basic narrowing. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 3.

How long does the “Union & Literal Types (basic narrowing intro)” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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