How does TypeScript "infer" work?


I know it was introduced in version 2.8 of the language (see here ), but despite having read these release notes, I still can't understand how infer works.

  • How it works?
  • What is your real objective?


infer is used in conjunction with type conditions in TypeScript because it will try to infer a type, and if that isn't possible, something must happen. So it must always be on a conditional type .

Basically, infer will "create" a new type, whose name will come in front of you:

infer NewType ? [[conseguiu-inferir]] : [[não-conseguiu-inferir]]

Where NewType will be the type that is inferred. In this sense, we will be able to use it in the "typical expression" that is positioned in [[conseguiu-inferir]] .

An example:

type GetArrayMemberType<T> = T extends Array<infer Member> ? Member : T;

type A = GetArrayMemberType<string[]>; // A inferido para `string`
type B = GetArrayMemberType<string[][]>; // B inferido para `string[]`
type C = GetArrayMemberType<number>; // C inferido para `number`

Link to the playground.

When conditional types are used in conjunction with infer (which must occupy the first operand of the "ternary conditional"), the type of the second operand (which is usually composed of the inferred type) is returned. If type inference is not possible, the third operand will be returned.

Think of this need for infer need to be associated with a conditional type relating the TypeScript type system to a purely functional mini programming language. That's because every TypeScript type must always be something (just like an expression in functional programming languages). And what if the type cannot be inferred and we don't provide an " else clause"? The resulting type should be what?

In short, just as a Haskell if always needs an else , a TypeScript conditional type always needs an else (which is located in the third term of the ternary construction) as well. This, associated with infer , guarantees that we will always have a valid type, even when inference is not possible.

See this structure:

//|             Primeiro operando. O `infer` deve ficar nele.
//|                                │
//|                                │           ╔ Nome que será atribuído ao tipo inferido pelo `infer`.
//|                               ┌┴───────────║────┐
//|                               │           ╔╩═══╗│
1 | type UnpackArr<T> = T extends Array<infer Member>
//|    ┌ O segundo operando da condição será retornado caso a inferência tenha sido possível.
//|    │ Geralmente, esse tipo é composto a partir daquele que foi inferido (no caso, `Member`).
//|   ┌┴─────────────┐
2 | ? { type: Member }
//|   ┌ O terceiro operando da condição será retronado caso a inferência não tenha sido possível.
//|   │ Neste caso, só estamos retornando o tipo passado (`T`).
3 | : T;

Note that a name must always be placed after the infer . It is the name that will be given to the type that will be inferred. The name can be used in the second operand of the type condition.

What is your real objective?

The goal is to be able to infer types. Most of the time it is used to "unpack" a type, getting the value of an "internal" type.

For example, as of the current TypeScript version when I write this response, the awaited keyword awaited not yet exist in TypeScript. Therefore, the only way to get the type T of a Promise<T> is from infer . Look:

type Awaited<T> = T extends Promise<infer Member> ? Member : T;

type A = Awaited<Promise<string>>; // A inferido `string`.
type B = Awaited<Promise<{ name: string, age: number }>>; // B inferido `{ name: string, age: number }`.

Link to the playground.

In the future, awaited Promise<T> (a construct that doesn't yet exist in TypeScript) will return type T , but for now the only way to achieve this is to use type conditionals in conjunction with infer .

Generally this is most useful for library authors or anything that requires a little more complex typing.

Scroll to Top