Course contentsShow
← TypeScript
Lesson 972 of 1,79622. The never TypeFree lesson

Building Exclude with never

Understanding how the built-in Exclude utility type uses never in a distributive conditional type.

Building Exclude with never

You'll learn: How TypeScript's built-in Exclude<T, U> utility type uses never in a distributive conditional type to filter out unwanted members from unions.

The Core Concept

The Exclude utility type removes specific types from a union. Here's the actual implementation:

type Exclude<T, U> = T extends U ? never : T;

This elegant one-liner leverages two powerful TypeScript features working together:

  1. Distributive conditional types — when T is a union, the conditional type distributes over each member
  2. never as the empty type — union members that resolve to never disappear from the final union

Think of it like a filter for a union type: each member passes through the conditional check individually, and members that match get replaced with never, effectively removing them.

How Distribution and never Work Together

When you write Exclude<"a" | "b" | "c", "b">, here's what happens:

// TypeScript distributes over each union member:
("a" extends "b" ? never : "a") |
("b" extends "b" ? never : "b") |
("c" extends "b" ? never : "c")

// Which evaluates to:
"a" | never | "c"

// never disappears from unions:
"a" | "c"

Each member is tested independently. Those matching U become never and vanish; those not matching pass through unchanged.

Why never Is Perfect Here

never acts as the "absence of a type" — when you union it with other types, it contributes nothing. This makes it the ideal return value for "exclude this member" logic in distributed conditionals.

Key Takeaway: Exclude<T, U> demonstrates how distributive conditional types and never combine to create type-level filtering — each union member flows through the extends check, unwanted matches resolve to never and disappear, leaving only the types you want.