0Pricing
TypeScript Academy · Lesson

Understanding infer in Conditional Types

Extract inner types using infer in extends clauses.

Understanding infer in Conditional Types is a free TypeScript Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is infer?

The infer keyword appears inside conditional types and lets TypeScript capture a type variable from the matched position, so you can use it in the true branch.

type GetReturn<T> = T extends (...args: any[]) => infer R ? R : never;

Basic infer Example

Here we extract the return type of any function using infer R.

type ReturnType<T> = T extends (...args: any[]) => infer R ? R : never;
type A = ReturnType<() => string>;  // string
type B = ReturnType<() => number[]>; // number[]

How infer Works

TypeScript matches the structure of T against the pattern. If it matches, the inferred variable holds the matched portion. If not, the false branch is taken.

type First<T> = T extends [infer F, ...any[]] ? F : never;
type X = First<[string, number, boolean]>; // string

Inferring from Generic Parameters

You can infer the type argument of a generic type, such as extracting T from Promise.

type Unwrap<T> = T extends Promise<infer U> ? U : T;
type A = Unwrap<Promise<string>>; // string
type B = Unwrap<number>;          // number

Multiple infer Variables

You can use multiple infer clauses in a single conditional type to capture several positions at once.

type Head<T> = T extends [infer H, ...infer Tail] ? H : never;
type Tail<T> = T extends [infer H, ...infer Tail] ? Tail : never;

infer in Function Parameters

Infer can capture function parameter types as well, not just return types.

type FirstParam<T> = T extends (first: infer P, ...rest: any[]) => any ? P : never;
type A = FirstParam<(x: string, y: number) => void>; // string

Distributive Behavior with infer

When T is a union, conditional types (including those with infer) distribute over each union member.

type UnwrapAll<T> = T extends Promise<infer U> ? U : T;
type X = UnwrapAll<Promise<string> | Promise<number>>;
// string | number

Nested infer

You can nest infer to reach deeper into a type structure, such as extracting the value type of an array inside a promise.

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

infer with String Patterns

Template literal types combined with infer let you extract substrings from string literal types.

type GetDomain<T extends string> =
  T extends `https://${infer Domain}` ? Domain : never;
type D = GetDomain<"https://example.com">; // "example.com"

Common Pitfalls

Infer only works inside the extends clause of a conditional type. Using it elsewhere causes a compile error. Also, infer in the false branch is not available.

// Error: infer can only appear in extends clauses
type Bad<T> = infer R; // SyntaxError

Recap: infer

The infer keyword lets TypeScript capture a type fragment from a structural match in a conditional type. It powers utility types like ReturnType, Parameters, and Awaited.

Quick Check

Where can the infer keyword be used?

What You Learned

The infer keyword enables type-level pattern matching by extracting type fragments from structural patterns. It is the foundation of powerful utility types and type-safe metaprogramming in TypeScript.

Frequently asked questions

Is the “Understanding infer in Conditional Types” lesson free?

Yes — the full text of “Understanding infer in Conditional Types” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Understanding infer in Conditional Types”?

Extract inner types using infer in extends clauses. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Understanding infer in Conditional Types” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Understanding infer in Conditional Types
  2. Building ReturnType and Parameters from Scratch
  3. Deeply Nested Inference Patterns
  4. Practical infer Use Cases: Unwrapping Promises
← Back to TypeScript Academy