Skip to main content
Module 6 · Lesson 1 of 211/22 in the course~18 min
Module lessons (1/2)

Conditional Types and infer

Lesson preview5 hands-on exercises readyCourse TypeScript · Module 6 — Conditional and Mapped Types

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 infer keyword.
  • 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

  1. 1Conditional Types and Distributivity
  2. 2Extract subtypes with infer
  3. 3Guided practice
  4. 4Check for understanding
Pro Lesson · Module 6 — Conditional and Mapped Types

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.