Skip to content

Infer

In one line

infer declares a type variable inside conditional types to extract and capture types from matched patterns.

In simple words

The infer keyword is used inside extends conditional types to extract a type from another type. It's like pattern matching at the type level — you declare a placeholder and TypeScript fills it in.

For example, type ElementOf<T> = T extends (infer E)[] ? E : never extracts the element type of an array. If T is string[], infer E captures string.

Common uses: extracting function return types, promise resolution types, array element types, component prop types. The ReturnType<T> utility type is built with infer.

You can have multiple infer variables and use them in the true branch of the conditional.

Example

typescript
// Extract array element type
type ElementOf<T> = T extends (infer E)[] ? E : never;
type R1 = ElementOf<string[]>; // string

// Extract function return type
type MyReturnType<T> = T extends (...args: any[]) => infer R ? R : never;
type R2 = MyReturnType<() => number>; // number

// Extract Promise resolution type
type Awaited<T> = T extends Promise<infer U> ? U : T;
type R3 = Awaited<Promise<string>>; // string

infer captures types from patterns: E from arrays, R from function returns, U from Promises. TypeScript fills in the inferred type.

Common confusions

  • Confused with: typeof

    The difference: typeof extracts the type of a value (runtime → type). infer extracts a type from another type (type → type) within conditional types. typeof works on values; infer works inside type-level conditional logic.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary