TypeScript 7 reference · Type Operators and Keywords

TypeScript infer Keyword

Inside a conditional type, infer captures part of a type into a new type variable, e.g. the element type of an array.

TypeScript 7 Since 2.8

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

T extends Something<infer U> ? U : Fallback

Examples

The first parameter's typeTry it yourself
type FirstArg<F> = F extends (first: infer A, ...rest: any[]) => any ? A : never;
function greet(name: string, times: number) {
  return name.repeat(times);
}
const arg: FirstArg<typeof greet> = "Ana";
console.log(greet(arg, 2));

Output

AnaAna
Unwrap a Promise yourselfTry it yourself
type Unpromise<T> = T extends Promise<infer V> ? V : T;
const v: Unpromise<Promise<number>> = 7;
console.log(v);

Output

7

Version history

VersionChange
TypeScript 2.8Added

Was this page helpful?