0Pricing
TypeScript Academy · Lesson

T extends U ? X : Y in practice

Write practical conditional types; learn distribution over unions and how to opt out when needed.

Intro

Goal: Use conditional types to express logic in the type system and understand when they distribute over unions.

  • Pattern: T extends U ? X : Y
  • Distribution: applies branch per union member

Basic pattern

Conditional types pick a branch based on whether T is assignable to string.

type IsString<T> = T extends string ? true : false

type A = IsString<string>
//   ^ true

type B = IsString<number>
//   ^ false

All lessons in this course

  1. T extends U ? X : Y in practice
  2. infer for ReturnType-like utilities
  3. Built-ins: ReturnType, Parameters, InstanceType, etc.
← Back to TypeScript Academy