Conditional Types + infer (Interview Essentials)
Understand conditional types, infer, and how TypeScript extracts types from functions/promises/arrays.
F
Frontend Interview Team
February 08, 2026
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.