← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Conditional Types & infer

typescript 2.8 Advanced

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

Added 16 Mar 2026
Edited 22 Mar 2026
Views 32
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 1 ping S 0 pings S 0 pings M 1 ping T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 1 ping S
Amazonbot 10 Perplexity 9 Ahrefs 2 Unknown AI 2 Google 1
crawler 23 pre-tracking 1
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

✓ schema.org compliant