# Conditional Types

> Writing 'if' statements for your types.

Subject: TypeScript | Lesson 28 of 54 | 5 min read
Interactive lesson: https://codewithanimation.in/typescript/conditional-types
Previous: [Indexed Access Types](https://codewithanimation.in/typescript/indexed-access-types.md) | Next: [The infer Keyword](https://codewithanimation.in/typescript/infer-keyword.md)

## The Type-Level Ternary

Conditional types allow you to create types that choose one of two possible outputs based on a condition.

They use the exact same ternary syntax as JavaScript (`condition ? true_branch : false_branch`), but instead of checking runtime values, they check type assignability.

## Dynamic Type Utilities

Without conditional types, you have to write a separate type for every possible scenario.

With conditional types, you can write highly dynamic, reusable utility types that automatically adjust their shape based on the type variable passed into them.

## Syntax

```typescript
// If T is a string, return 'yes', otherwise return 'no'.
type IsString<T> = T extends string ? "yes" : "no";

type A = IsString<"hello">; // "yes"
type B = IsString<42>;      // "no"

// A more realistic example:
// If T is an array, extract its element type. Otherwise, leave it alone.
type Flatten<T> = T extends any[] ? T[number] : T;

type C = Flatten<string[]>; // string
type D = Flatten<number>;   // number
```

## Common mistake: Distributive Conditional Types

When you pass a *Union Type* (e.g., `string | number`) into a conditional type, it doesn't evaluate the whole union at once.

Instead, it evaluates each part of the union individually, and unions the results: `IsString<string | number>` becomes `IsString<string> | IsString<number>`, resulting in `"yes" | "no"`.

## Tip: Chaining Conditionals

Just like in JavaScript, you can chain ternary operators to create complex type-level `if/else if/else` statements: `T extends string ? A : T extends number ? B : C`.

## Try it: Try it

Pass different types into the `IsString<T>` conditional type evaluator and watch how the ternary logic resolves to the true or false branch.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/typescript/conditional-types

## Check yourself

### Question 1

What does `type Result = 42 extends number ? true : false` evaluate to?

- A. true
- B. false
- C. Error: 42 is a value, not a type.

Correct answer: A

The literal type 42 satisfies the condition of being a number, so the true branch is evaluated.

- Why not B: The literal type 42 is assignable to the generic 'number' type.
- Why not C: In TypeScript, literal values like 42 or 'hello' are also valid types.

## Remember this

- Conditional types use the syntax `T extends U ? X : Y`.
- The condition `T extends U` checks if `T` is assignable to `U`.
- When given a union type, conditional types distribute across the union automatically.
- They are the foundation of advanced TypeScript utility types.
