infer Keyword in Conditional Types
TL;DR
The infer keyword inside conditional types lets TypeScript extract and name a type from within a generic structure — enabling ReturnType<T>, Awaited<T>, and custom type extraction.
Explanation
infer is used in the extends clause of conditional types: type ReturnType<T> = T extends (...args: any[]) => infer R ? R : never. Here infer R captures the return type. Built-in uses: ReturnType<T>, Parameters<T>, InstanceType<T>, Awaited<T>. Custom uses: extracting element type from arrays (T extends (infer U)[] ? U : never), unwrapping Promises, extracting function argument types. infer can only appear on the right-hand side of extends. Multiple infer in one conditional is supported.
Common Misconception
✗ infer extracts types at runtime — it's purely a compile-time type-level operation with no runtime cost or representation.
Why It Matters
infer enables powerful generic utilities that can derive types from other types — the foundation of TypeScript's built-in utility types.
Common Mistakes
- Using infer outside conditional types — syntax error.
- Not knowing infer captures the narrowest type possible.
- Forgetting the never fallback — conditional types need both branches.
Code Examples
✗ Vulnerable
// Without infer — can't extract return type generically:
type GetReturn<T> = any; // No way to extract without infer
✓ Fixed
// Built-in pattern:
type ReturnType<T> = T extends (...args: any[]) => infer R ? R : never;
// Custom — unwrap array element type:
type ArrayElement<T> = T extends (infer U)[] ? U : never;
type Item = ArrayElement<string[]>; // string
// Custom — unwrap Promise:
type Unwrap<T> = T extends Promise<infer U> ? U : T;
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
23 Mar 2026
Views
30
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Amazonbot 12
Perplexity 5
Unknown AI 3
Google 3
Ahrefs 3
ChatGPT 1
Meta AI 1
How they use it
crawler 26
crawler_json 1
pre-tracking 1
Related categories
⚡
DEV INTEL
Tools & Severity
🔵 Info
⚙ Fix effort: Medium
⚡ Quick Fix
Use infer in conditional types to extract sub-types. Pattern: T extends SomeWrapper<infer U> ? U : never. Combine with ReturnType/Parameters for function type utilities.
📦 Applies To
typescript 2.8
web
cli
🔗 Prerequisites
🔍 Detection Hints
infer
Auto-detectable:
✗ No
typescript
🤖 AI Agent
Confidence: Low
False Positives: High
✗ Manual fix
Fix: High
Context: File