Skip to content

Conditional Types

Spicy — senior dev territoryGeneral Dev

ELI5 — The Vibe Check

Conditional types are if-else statements for TypeScript's type system. 'If T extends string, give me a number, otherwise give me a boolean.' It's programming inside the type system, and yes, it gets wild.

Real Talk

A TypeScript type-level construct using the syntax T extends U ? X : Y that selects a type based on a type condition. Conditional types enable type-level pattern matching, are distributive over union types, support inference with 'infer' keyword, and power advanced type manipulation patterns.

Show Me The Code

type IsString<T> = T extends string ? true : false;

type Flatten<T> = T extends Array<infer U> ? U : T;
type A = Flatten<string[]>;  // string
type B = Flatten<number>;    // number

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

When You'll Hear This

"We use conditional types with infer to extract the return type of async functions automatically." / "Conditional types are TypeScript's superpower — they let you write type-level programs."

Made with passive-aggressive love by manoga.digital. Powered by Claude.