0Pricing
TypeScript Academy · Lesson

Conditional Types: T extends U ? X : Y

Write type-level if statements with conditional types.

Conditional Types: T extends U ? X : Y is a free TypeScript Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Welcome

Conditional types let you express type-level if statements. They are a core part of TypeScript's advanced type system used to build utility types.

Basic Syntax

Write T extends U ? X : Y. If T is assignable to U, the result is X; otherwise Y.
type IsString<T> = T extends string ? true : false;
type A = IsString<string>; // true
type B = IsString<number>; // false

NonNullable Built-In

NonNullable is implemented with a conditional type that removes null and undefined from T.
type NonNullable<T> = T extends null | undefined ? never : T;
type C = NonNullable<string | null>; // string

Conditional with Generics

Conditional types are most useful inside generic functions and type aliases.
type Flatten<T> = T extends Array<infer I> ? I : T;
type Str = Flatten<string[]>; // string
type Num = Flatten<number>;   // number

Nested Conditionals

Conditional types can be nested for more complex logic.
type TypeName<T> =
  T extends string ? 'string' :
  T extends number ? 'number' :
  T extends boolean ? 'boolean' :
  'object';

Distribution Over Unions

When T is a bare type parameter, a conditional type distributes over union members.
type ToArray<T> = T extends any ? T[] : never;
type R = ToArray<string | number>; // string[] | number[]

Preventing Distribution

Wrap T in a tuple to prevent distribution.
type NoDistribute<T> = [T] extends [any] ? T[] : never;
type R2 = NoDistribute<string | number>; // (string | number)[]

Using never to Filter

Returning never from a conditional type removes union members.
type OnlyStrings<T> = T extends string ? T : never;
type S = OnlyStrings<'a' | 'b' | 1 | 2>; // 'a' | 'b'

Conditional Types in Mapped Types

Combine mapped types and conditional types to transform object properties selectively.
type NullableValues<T> = { [K in keyof T]: T[K] extends string ? null : T[K] };

Inferring from Function Parameters

Use infer in conditional types to extract parts of complex types.
type FirstParam<T> = T extends (first: infer P, ...rest: any[]) => any ? P : never;
type FP = FirstParam<(a: string, b: number) => void>; // string

Built-In Conditional Utilities

Many built-in utilities (ReturnType, Parameters, InstanceType) are built with conditional types and infer.
type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;

Quick Check

What is the result of `type R = string extends object ? 'yes' : 'no'`?

Recap

Conditional types (T extends U ? X : Y) enable type-level logic. They distribute over unions, work with infer for extraction, and power many of TypeScript's built-in utility types.

Frequently asked questions

Is the “Conditional Types: T extends U ? X : Y” lesson free?

Yes — the full text of “Conditional Types: T extends U ? X : Y” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Conditional Types: T extends U ? X : Y”?

Write type-level if statements with conditional types. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Conditional Types: T extends U ? X : Y” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. keyof and Indexed Access Types
  2. Generic Constraints: Narrowing Type Parameters
  3. Conditional Types: T extends U ? X : Y
  4. Distributive Conditional Types
← Back to TypeScript Academy