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

The Type Predicate Pattern

Use conditional types to create boolean-like type checks that return true or false types based on conditions.

The Type Predicate Pattern

What you'll learn: How to create boolean-like type checks using conditional types that return true or false type literals based on conditions.

The Core Idea

You've already learned that conditional types can perform type-level "if-else" logic with T extends U ? X : Y. The Type Predicate Pattern takes this further by making conditional types return true or false literal types — essentially creating compile-time boolean checks that TypeScript can reason about.

Think of it like writing a function that returns a boolean, except this "function" runs entirely in TypeScript's type system and returns type-level true or false values.

How It Works

The pattern uses conditional types that resolve to literal boolean types:

type IsString<T> = T extends string ? true : false;

type A = IsString<string>;    // true
type B = IsString<number>;    // false
type C = IsString<"hello">;   // true

You can combine these with other conditional types to make decisions:

type IsArray<T> = T extends unknown[] ? true : false;

type CheckArray<T> = IsArray<T> extends true 
  ? "It's an array!" 
  : "Not an array";

type Result1 = CheckArray<string[]>;  // "It's an array!"
type Result2 = CheckArray<number>;    // "Not an array"

This pattern becomes powerful when building complex type-level logic where you need to test properties and branch accordingly.

When to Use This Pattern

The type predicate pattern shines when you're composing multiple type-level checks or need to store boolean test results as types themselves. It makes your type logic more readable by breaking complex conditions into named, testable pieces.

Key Takeaway: Use conditional types returning true or false literal types to create reusable, boolean-like type checks that make complex type logic more readable and composable.