Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Union & Intersection Types | TypeScript Mastery lesson | BraveProgrammer
  1. Courses
  2. /
  3. TypeScript Mastery

Lesson 4 of 56

·Typescript

Union & Intersection Types

20 minutes

Course progress7%

Union Types (|)

A value can be one of several types.

type Status = "success" | "error" | "pending";
let s: Status = "success"; 
s = ; 
// OK
"loading"
// ❌ Not assignable

Intersection Types (&)

A value must satisfy all constituent types.

interface A { a: string; }
interface B { b: number; }
type AB = A & B; // must have both a and b
const obj: AB = { a: "foo", b: 123 };

Practical example – API response

type Success = { ok: true; data: string; };
type Failure = { ok: false; error: string; };
type ApiResult = Success | Failure;

function handle(r: ApiResult) {
  if (r.ok) {
    console.log(r.data); // `data` is guaranteed here
  } else {
    console.error(r.error);
  }
}

Info

Use unions for variant data and intersections for mix‑ins.
Make it stickPractice what you've learned1 quiz · 3 practical tests · Optional activities from TypeScript MasteryBrowse activities
Previous lessonPrimitive Types & Type InferenceContinue to lesson 5Literal Types & EnumsBack to TypeScript Mastery curriculum