Module lessons (1/2)
Conditional Types and infer
Evaluate branches T extends U ? X : Y on distributive types and extract sub-types with infer.
What you'll learn in this lesson
- Write type-level decisions using the ternary operator
T extends U ? X : Y. - Understand how
Exclude<T, U>,Extract<T, U>and distributivity work internally on naked unions. - Extract internal types of arrays, Promises and function parameters/returns using the
inferkeyword. - Avoid unwanted distributivity by enclosing both sides in tuples (
[T] extends [U]).
Overview
Conditional Types bring the if / else logic directly into the type system: TipoA extends TipoB ? SeVero : SeFalso. Combined with infer, they allow you to unpack complex types without forcing the function caller to repeat them.
When a bare type parameter (T) receives a union A | B, TypeScript applies the condition to each member separately and then merges the results!
Sections covered in this lesson
- 1Conditional Types and Distributivity
- 2Extract subtypes with
infer - 3Guided practice
- 4Check for understanding
Unlock "Conditional Types and infer" and the full Course TypeScript curriculum
Evaluate branches T extends U ? X : Y on distributive types and extract sub-types with infer.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in Course TypeScript plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.