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

never as a Signal in Type Logic

Using never intentionally in conditional types and mapped types to filter or exclude possibilities.

never as a Signal in Type Logic

You'll learn: How to use never intentionally in conditional and mapped types to filter out or exclude unwanted type possibilities.

The Core Idea

In advanced TypeScript, never isn't just for marking impossible states—it's a powerful signal you can send through your type system. When you deliberately return never from a conditional type or map a property to never, TypeScript treats it as "this option doesn't exist" and removes it from unions or object types.

Think of never like a "void check" in a contest: if a contestant doesn't meet the criteria, they're never considered—they simply disappear from the pool of candidates. TypeScript does the same with types.

How never Signals "Remove This"

When never appears in a union type, TypeScript eliminates it:

type Example = string | never | number;
// Resolves to: string | number
// The never vanishes!

This behavior makes never perfect for filtering types.

Filtering with Conditional Types

You can use never to exclude types from a union that don't meet certain criteria:

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

type Mixed = string | number | boolean;
type Filtered = RemoveStrings<Mixed>;
// Result: number | boolean
// All string types became never and disappeared

The conditional type distributes over the union, and wherever T extends string is true, we return never—signaling "exclude this."

never in Mapped Types for Property Filtering

You can use never to remove properties from object types by remapping keys to never:

type RemoveOptionalProps<T> = {
  [K in keyof T as undefined extends T[K] ? never : K]: T[K]
};

type User = {
  id: number;
  name: string;
  nickname?: string;
};

type RequiredOnly = RemoveOptionalProps<User>;
// Result: { id: number; name: string }
// nickname key remapped to never, so it's excluded

When a key remaps to never, TypeScript removes that property entirely from the resulting type.

Real-World Pattern: Extract Matching Types

type ExtractFunctions<T> = {
  [K in keyof T as T[K] extends Function ? K : never]: T[K]
};

type API = {
  getUser: () => void;
  userName: string;
  deleteUser: () => void;
  userId: number;
};

type Methods = ExtractFunctions<API>;
// Result: { getUser: () => void; deleteUser: () => void }

Properties that aren't functions get mapped to never and vanish.

Key Takeaway: Use never as an intentional signal in conditional and mapped types to filter unions or remove object properties—TypeScript will eliminate never types automatically, giving you precise type transformations.