0Pricing
TypeScript Academy · Lesson

Intersection and Union Types

Understand how to combine multiple types into one using intersections and create flexible type options with unions.

Intersection and Union Types is a free TypeScript Academy lesson on CoddyKit — 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, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

Introduction to Intersection and Union Types

Intersection and union types in TypeScript allow you to combine or create flexible types, providing a powerful way to define complex data structures.

Intersection and Union Types — illustration 1

2

What are Union Types?

Union types allow a value to be one of several types. You can define a union type using the | operator.

3

Example of Union Types

Here’s an example of using a union type:

type Status = 'active' | 'inactive' | 'suspended';

let userStatus: Status;
userStatus = 'active'; // Valid
userStatus = 'pending'; // Error

4

What are Intersection Types?

Intersection types combine multiple types into one, requiring the combined type to have all properties of the intersected types.

5

Example of Intersection Types

Here’s an example of using an intersection type:

type User = {
  name: string;
  email: string;
};

type Admin = {
  role: string;
};

type AdminUser = User & Admin;

const admin: AdminUser = {
  name: 'Alice',
  email: 'alice@example.com',
  role: 'admin'
};

6

Differences Between Union and Intersection Types

Union types allow values to be of multiple possible types, while intersection types combine multiple types into one, requiring all properties to be present.

7

Practical Use Cases

Union types are great for handling multiple input types, while intersection types are ideal for combining multiple roles or features into one type.

8

9

Practice Task

Create a type that combines a Product type with a Discount type using intersection types. Use it to declare a variable.

10

Congratulations!

You’ve learned how to use union and intersection types to define flexible and combined types in TypeScript. Practice these concepts to make your code more robust and type-safe!

Intersection and Union Types — illustration 10

Frequently asked questions

Is the “Intersection and Union Types” lesson free?

Yes — the full text of “Intersection and Union Types” is free to read here on the web, and the TypeScript Academy course includes 3 lessons in total. 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.

What will I learn in “Intersection and Union Types”?

Understand how to combine multiple types into one using intersections and create flexible type options with unions. 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; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Intersection and Union Types” 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. Keyof and Lookup Types
  2. Mapped Types
  3. Intersection and Union Types
← Back to TypeScript Academy