Course contentsShow
← TypeScript
Lesson 766 of 1,79618. Conditional TypesFree lesson

The extends Keyword in Conditionals

Understand how extends checks type compatibility in conditional types, different from constraint extends.

The extends Keyword in Conditionals

What you'll learn: How extends in conditional types performs type compatibility checks—a different job than the constraint extends you've used before.

The Two Faces of extends

You've already seen extends used to constrain generic type parameters (like <T extends string>). In conditional types, extends does something different: it tests whether one type is assignable to another, then picks a branch based on the result.

How It Works

The syntax looks like this:

T extends U ? TrueType : FalseType

TypeScript checks: "Can T be assigned to U?" If yes, the whole expression evaluates to TrueType. If no, it evaluates to FalseType.

Think of it like a type-level if/else statement. The extends keyword here is asking a question about compatibility, not imposing a restriction.

Simple Example

type IsString<T> = T extends string ? "yes" : "no";

type A = IsString<string>;    // "yes"
type B = IsString<number>;    // "no"
type C = IsString<"hello">;   // "yes" (literal extends string)

Here, extends checks if the type you pass matches string. It's not constraining T—it's testing it.

The Key Difference

  • Constraint extends: "T must be assignable to U" — enforces a requirement
  • Conditional extends: "Is T assignable to U?" — asks a question, returns a type based on the answer

Key Takeaway: In conditional types, extends is a compatibility test that chooses between two type outcomes, not a constraint that limits what types you can pass in.