ReviseAlgo Logo

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 PatternSyntaxResult when T = string[]
Type CheckT extends string ? A : BB
Array CheckT extends any[] ? A : BA
| Infer Item | T extends (infer U)[] ? U : T | string |