infer
Infer inside conditions
infer declares a fresh type variable inside a condition and captures whatever sits in its spot. Think of it as pattern matching for types.
First matches an array of something, names that something E, and returns it. A non-array falls through to never.
type First<T> = T extends Array<infer E> ? E : never; const f: First<Array<string>> = "a"; console.log(typeof f);
string
Infer return types
Return positions work the same way. Match the function shape, capture the result with infer R, and return it.
This mirrors the built-in ReturnType, where the any array accepts every argument list. N resolves to string for a function that returns a string.
type Returns<F> = F extends (...args: any[]) => infer R ? R : never; const n: Returns<(id: number) => string> = "ok"; console.log(n);
ok
Infer arguments
Parameter positions can be captured too. Match the first parameter with infer A and let the rest collect the remainder.
S resolves to string because the example takes a name first. This is the same idea behind the built-in Parameters helper.
type FirstArg<F> = F extends (first: infer A, ...rest: unknown[]) => unknown ? A : never; const s: FirstArg<(name: string) => number> = "Ada"; console.log(s);
Ada
Unwrap promises
Promises nest, and infer can peel every layer with recursion. If the type is a Promise, capture the inside and unwrap again, else stop.
U resolves to plain string no matter how deep the nesting goes. The built-in Awaited type follows the same recipe.
type Unwrap<T> = T extends Promise<infer V> ? Unwrap<V> : T; const u: Unwrap<Promise<Promise<string>>> = "done"; console.log(u);
done
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
inferdo inside a conditional type?Answer
It declares a type variable and captures the part that matches its spot
The condition matches a shape, and infer names the piece the shape leaves open.
-
What does
First<Array<string>>resolve to fortype First<T> = T extends Array<infer E> ? E : never?Answer
stringThe array shape matches, and E captures the element type inside it.
-
What does
Returns<F>capture from a function type?Answer
Its return type
The arrow shape matches the arguments, and R captures what the function returns.
-
How does
Unwrap<T>remove several layers ofPromise?Answer
It captures the inside and calls itself on that type
The conditional type recurses on the captured inner type until it is no longer a Promise.
-
What does this program print?
type First<T> = T extends Array<infer E> ? E : never; const f: First<Array<string>> = "a"; console.log(typeof f);Answer
string
First resolves to string, and the constant holds a string value.
-
What does this program print?
type Returns<F> = F extends (...args: any[]) => infer R ? R : never; const n: Returns<(id: number) => string> = "ok"; console.log(n);Answer
ok
The return type is string, so the constant holds the text ok.
-
What does this program print?
type FirstArg<F> = F extends (first: infer A, ...rest: unknown[]) => unknown ? A : never; const s: FirstArg<(name: string) => number> = "Ada"; console.log(s);Answer
Ada
The first parameter is a string, so the constant holds Ada.
-
What does this program print?
type Unwrap<T> = T extends Promise<infer V> ? Unwrap<V> : T; const u: Unwrap<Promise<Promise<string>>> = "done"; console.log(u);Answer
done
Both promise layers are peeled away, leaving string, so the constant holds done.
-
What does the compiler say about this program?
type Bad<T> = infer E; console.log("x");Answer
error TS1338: 'infer' declarations are only permitted in the 'extends' clause of a conditional type.
infer only works on the right side of extends inside a conditional type.
-
What does the compiler say about this program?
type First<T> = T extends Array<infer E> ? E : never; const f: First<Array<number>> = "a"; console.log(f);Answer
error TS2322: Type 'string' is not assignable to type 'number'.
First resolves to number for an array of numbers, so the string cannot fill it.
Article author: Arthur Isaev