Conditional Types + infer (Interview Essentials)
Understand conditional types, infer, and how TypeScript extracts types from functions/promises/arrays.
On this page
30‑second interview answer
Conditional types let TypeScript compute a type based on a condition (T extends U ? X : Y). infer lets you capture part of a type inside a conditional (like extracting a promise’s resolved type). They power many utility types and advanced generic APIs.
Conditional types
type IsString<T> = T extends string ? true : false;
type A = IsString<'x'>; // true
type B = IsString<123>; // falseinfer: extract inner types
Extract array element
type Element<T> = T extends (infer U)[] ? U : never;
type X = Element<string[]>; // stringExtract promise resolved type
type AwaitedLike<T> = T extends Promise<infer U> ? U : T;
type R1 = AwaitedLike<Promise<number>>; // number
type R2 = AwaitedLike<string>; // stringReal-world use: function return type
type Return<T> = T extends (...args: any[]) => infer R ? R : never;Common pitfalls
- Distributive conditional types on unions
- Getting
neverunexpectedly
Interview-safe line:
“These are powerful, but I keep them small and tested. I prefer clarity over clever.”
Mini Q&A
Q1: What is a conditional type?
- A type-level if/else.
Q2: What does infer do?
- Captures a type variable from inside another type.
Q3: Where are these used?
- Utility types, libraries like React Query, typed APIs.
Summary checklist
- I can write a conditional type.
- I can use infer to extract inner types.
- I understand union distribution basics.
Looking for structured, offline interview prep?
Free online lessons • Portable Amazon Kindle / Paperback
Frontend Interview Guide (2025 Edition)
By Rajesh Dhiman
Comprehensive preparation covering React internals, Web Performance, CSS architecture, and system design questions.
JavaScript Interview Guide
By Rajesh Dhiman
Deep dive into the Event Loop, microtask draining, closures, prototypal inheritance, and asynchronous execution patterns.
Note: All lessons on Frontend Interview remain completely free to read online. As an Amazon Associate, we may earn from qualifying purchases.
Written by Rajesh Dhiman
Authored by Rajesh Dhiman and engineered at Eunix Tech. Focused on deep-dive frontend engineering, JavaScript runtime internals, React architecture, and web performance.