T extends U ? X : Y in practice
Write practical conditional types; learn distribution over unions and how to opt out when needed.
T extends U ? X : Y in practice is a free TypeScript Academy lesson on CoddyKit — lesson 1 of 3. 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 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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>
// ^ falseDistribution
With a naked T, the check runs per union member: string | number becomes string[] | number[].
type ToArray<T> = T extends any ? T[] : never
type C = ToArray<string | number>
// distributes:
// C is string[] | number[]Opt out
Wrap T in a tuple ([T]) to prevent distribution and treat the union as a whole.
type ToArrayNoDist<T> = [T] extends [any] ? T[] : never
type D = ToArrayNoDist<string | number>
// no distribution:
// D is (string | number)[]Real-world utility
Example of the pattern: For arrays, ElementType uses infer to extract the element; otherwise, it leaves the type as is.
type NonEmptyArray<T> = T[] extends [] ? never : T[]
// Usually better:
// type ElementType<T> = T extends ReadonlyArray<infer U> ? U : T
type E1 = ElementType<string[]>
// ^ string
type E2 = ElementType<readonly number[]>
// ^ number
type E3 = ElementType<string>
// ^ stringTips
Tips: Keep conditions simple; note the deployment; reuse helper types; include test files for complex combinations.
// Tips
// - Keep conditions simple; deep nesting harms readability
// - Document distribution behavior for teammates
// - Prefer helper aliases (ElementType, Awaited) for reuse
// - Watch for De Morgan like logic when composing unionsDistribution check
Quick check: When do conditional types distribute?
Recap
Recap: Conditional types model logic in types. Distribution applies per union member; use tuple wrap to opt out.
Frequently asked questions
Is the “T extends U ? X : Y in practice” lesson free?
Yes — the full text of “T extends U ? X : Y in practice” is free to read here on the web, and the TypeScript Academy course includes 3 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 “T extends U ? X : Y in practice”?
Write practical conditional types; learn distribution over unions and how to opt out when needed. 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 1 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “T extends U ? X : Y in practice” 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
- T extends U ? X : Y in practice
- infer for ReturnType-like utilities
- Built-ins: ReturnType, Parameters, InstanceType, etc.