Conditional Types
Use conditional types to select types based on conditions, similar to if/else for types.
David Miller
November 20, 2025
1.9k52
Conditional types work like type-level if/else.
## Syntax
```ts
T extends U ? X : Y
```
## Example
```ts
type IsString<T> = T extends string ? true : false;
type A = IsString<string>; // true
type B = IsString<number>; // false
```
## Practical example
```ts
type ApiResult<T> = T extends Error ? { ok: false } : { ok: true; data: T };
```
## Graph
```mermaid
flowchart TD
A[T] --> B{extends?}
B -->|Yes| C[Type X]
B -->|No| D[Type Y]
```
## Remember
- Conditional types adapt APIs
- Powerful for libraries
#TypeScript#Intermediate#Conditional Types