Conditional Types & infer
Also Known As
conditional types
infer keyword
TypeScript type inference
TL;DR
Types that choose between two types based on a condition — T extends U ? X : Y — enabling type-level branching. The infer keyword extracts types from within a conditional.
Explanation
Conditional types: T extends Condition ? TrueType : FalseType. Distributive conditional types: when T is a union, the condition distributes over each member. The infer keyword captures a type variable within the extends clause: T extends Promise<infer U> ? U : never extracts the resolved type of a Promise. Built-in TypeScript utilities using conditional types: ReturnType<T>, Parameters<T>, InstanceType<T>, Awaited<T>. Essential for building type utilities that work with generic type shapes.
Common Misconception
✗ Conditional types are too complex to be practical — ReturnType<T>, Parameters<T>, and Awaited<T> are all conditional types used daily; they are essential for framework types and generic utilities.
Why It Matters
Without conditional types, you cannot express that a function returns the resolved type of a promise, or that a generic utility behaves differently for arrays vs non-arrays — conditional types unlock these patterns.
Common Mistakes
- Forgetting distributivity — T extends string ? true : false where T = string | number gives boolean, not true.
- Infinite type recursion — conditional types can recurse; TypeScript limits depth.
- Using conditional types where generics or overloads are cleaner.
- Not using infer when extracting nested types — manual type indexing is error-prone.
Code Examples
✗ Vulnerable
// Manual return type extraction — not generic:
type StringFuncReturn = string;
type NumberFuncReturn = number;
// Need separate type for every function signature
✓ Fixed
// Conditional type with infer — works for any function:
type ReturnType<T extends (...args: any) => any> =
T extends (...args: any) => infer R ? R : never;
// Unwrap Promise:
type Awaited<T> = T extends Promise<infer U> ? Awaited<U> : T;
// Extract array element type:
type ElementType<T> = T extends (infer U)[] ? U : never;
type A = ElementType<string[]>; // string
type B = Awaited<Promise<number>>; // number
// Distributive:
type NonNullable<T> = T extends null | undefined ? never : T;
type C = NonNullable<string | null | undefined>; // string
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
16 Mar 2026
Edited
22 Mar 2026
Views
32
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 1
Amazonbot 10
Perplexity 9
Ahrefs 2
Unknown AI 2
Google 1
Also referenced
How they use it
crawler 23
pre-tracking 1
Related categories
⚡
DEV INTEL
Tools & Severity
🟢 Low
⚙ Fix effort: High
⚡ Quick Fix
Use conditional types T extends U ? X : Y for type-level if statements — infer keyword within conditional types lets you extract type information from complex generics
📦 Applies To
typescript 2.8
web
cli
🔗 Prerequisites
🔍 Detection Hints
Function overloads that could be replaced by a single generic signature with conditional return type; type any used where conditional type would narrow correctly
Auto-detectable:
✓ Yes
typescript
eslint
⚠ Related Problems
🤖 AI Agent
Confidence: Low
False Positives: High
✗ Manual fix
Fix: High
Context: File