Generics
Conditional Types with Generics
Master Conditional Types (T extends U ? X : Y) with Generics in TypeScript, type resolution based on generic inputs, and practical type branching.
Last Updated: July 29, 2026
•
10 min read
Conditional Types allow you to choose one of two possible types based on a type relationship check. Syntax follows JavaScript's ternary operator:When combined with generics, conditional types allow TypeScript to branch types dynamically based on input parameter characteristics.
1. Basic Conditional Type Syntax
2. Dynamic Function Return Types
Conditional types enable functions to return different types depending on parameter types:
3. Extracting Array Element Types
4. Interactive Code Playground
Test conditional types below:
5. Summary Table
| Conditional Pattern | Syntax | Result when T = string[] |
|---|---|---|
| Type Check | T extends string ? A : B | B |
| Array Check | T extends any[] ? A : B | A |
T extends (infer U)[] ? U : T | string |