Skip to content

Latest commit

 

History

History
12 lines (8 loc) · 546 Bytes

infer-keyword.md

File metadata and controls

12 lines (8 loc) · 546 Bytes

The infer keyword allows you to deduce a type from another type within a conditional type.

type ReturnType<T> = T extends (infer R)[] ? R : T;

type t1 = ReturnType<number[]>; // Infer R from number[]

type t2 = ReturnType<string>; // Infer T from string

In the above example, the conditional type checks whether the type is an array. If it is, the type R is inferred from the array and returned. Otherwise, the type T is returned.
The infer keyword is often used in conjunction with the extends keyword.