Conditional Types
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."
Related Terms
Generic Constraints
Generic constraints are like telling a restaurant 'I'll eat anything... as long as it has cheese.
Mapped Types
Mapped types let you create new types by transforming every property of an existing type.
TypeScript
TypeScript is JavaScript with a strict parent watching over it.
Utility Types
Utility types are TypeScript's built-in type transformers — like Instagram filters but for your interfaces.