Lesson

TypeScript Conditional Types

10 min reading
Free Course

TypeScript Conditional Types: Logic Inside the Type System

A TypeScript conditional type works like a ternary operator for types: T extends U ? TrueType : FalseType. If type T is assignable to U, the expression resolves to TrueType; otherwise it resolves to FalseType.

Conditional types power standard utilities like NonNullable<T>, Exclude<T, U>, and ReturnType<T>, and let library authors map input types dynamically to exact output types.

Conditional Branching and infer

The infer keyword declares a temporary type variable inside the extends clause so you can extract nested types—such as pulling the element type out of an array or the resolved type out of a Promise.

type ElementType<T> = T extends readonly (infer U)[] ? U : T;
type UnwrapPromise<T> = T extends Promise<infer V> ? V : T;

type A = ElementType<string[]>;          // string
type B = ElementType<number>;            // number
type C = UnwrapPromise<Promise<boolean>>; // boolean

function firstOrSelf<T>(input: T): ElementType<T> {
  return (Array.isArray(input) ? input[0] : input) as ElementType<T>;
}

console.log(firstOrSelf(["alpha", "beta"]), firstOrSelf(42));

Distributive Behavior Over Unions

When T is a naked generic parameter passed a union (string | number), T extends string ? "text" : "other" distributes across each union member individually, yielding "text" | "other". Wrap [T] extends [string] in brackets to disable distribution when needed.

Common Mistakes to Avoid

  • Expecting TypeScript to resolve uninstantiated conditional types inside function bodies: Inside a generic function, tsc often cannot verify concrete values against an unresolved conditional return type, requiring careful helper assertions or overloads.
  • Forgetting distributive surprises on never: Passing never into a distributive conditional type returns never directly instead of running the false branch unless wrapped in [T].
  • Over-nesting 4-level conditional types for simple app code: Save complex conditional types for shared helpers and library utilities; keep everyday business code plain and readable.

Frequently Asked Questions

Where can the infer keyword be used?

infer is valid only inside the extends condition of a conditional type (T extends ... ? ... : ...).

How does ReturnType<T> work under the hood?

It uses type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;.

What does Exclude<T, U> look like as a conditional type?

type Exclude<T, U> = T extends U ? never : T;, which distributes across union T and drops any member matching U.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
TypeScript Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum