Course contentsShow
← TypeScript
Lesson 772 of 1,79618. Conditional TypesFree lesson

never in Conditional Types

Use never in conditional branches to filter out unwanted types from unions and other structures.

never in Conditional Types

What you'll learn: How to use never in conditional type branches to filter out unwanted types from unions and other type structures.


The Core Idea

In conditional types, the never type acts like a type-level filter that removes possibilities you don't want. When a conditional type branch resolves to never, TypeScript effectively discards that possibility from the final result—especially powerful when working with unions.

Think of never like a sieve: when you pour a union through a conditional type that returns never for certain cases, those cases disappear, leaving only what you want.


Filtering Unions with never

The most common pattern uses never to keep only certain types from a union:

type RemoveStrings<T> = T extends string ? never : T;

type Example = RemoveStrings<string | number | boolean>;
// Result: number | boolean
// The string case returned never, so it vanished

Because conditional types distribute over unions, each member is tested individually. When string is tested, it matches the condition and returns never—which gets filtered out of the final union.


Practical Filtering Pattern

type OnlyFunctions<T> = T extends (...args: any[]) => any ? T : never;

type Mixed = string | number | ((x: number) => void) | (() => string);
type Fns = OnlyFunctions<Mixed>;
// Result: ((x: number) => void) | (() => string)

Here, non-function types all resolve to never and disappear, leaving only the function types.


Key Takeaway: Use never in conditional type branches to filter unwanted types from unions—it acts as a type-level removal mechanism that makes certain possibilities disappear from the final result.