Sub topic 7
Extracting types with infer
This chapter assumes you know conditional types (T extends U ? X : Y), generics with constraints, and the basic function and tuple types. You should also be comfortable with Promise and rest parameters.
The infer keyword declares a type variable inside the extends clause of a conditional type. TypeScript fills that variable with whatever part of the checked type matches the pattern. You can then use the variable in the true branch. Without infer, you can only test whether a type matches. With infer, you can also pull a piece out of it.
The basic pattern
Write infer U where you want to capture a type, and give it a name. The compiler binds U only when the check succeeds.
type ElementOf<T> = T extends readonly (infer U)[] ? U : never;
type E1 = ElementOf<number[]>; // number
type E2 = ElementOf<string[]>; // string
type E3 = ElementOf<string>; // neverFor number[], the compiler matches readonly (infer U)[] and sets U to number. For string, the pattern does not match, so the false branch returns never.
The same idea extracts a return type:
type MyReturnType<T> = T extends (...args: any[]) => infer R ? R : never;
type R1 = MyReturnType<() => Promise<string>>; // Promise<string>
type R2 = MyReturnType<(n: number) => boolean>; // booleanUnwrapping and taking apart tuples
You can match a wrapper type to remove it:
type Unwrap<T> = T extends Promise<infer U> ? U : T;
type P1 = Unwrap<Promise<string>>; // string
type P2 = Unwrap<boolean>; // booleanTuples work the same way. This takes the first element of a tuple:
type Head<T> = T extends [infer H, ...unknown[]] ? H : never;
type H1 = Head<[1, 2, 3]>; // 1
type H2 = Head<[]>; // neverExtracting the parameter list of a function gives a tuple type:
type Params<T> = T extends (...args: infer P) => unknown ? P : never;
type Fn = (name: string, age: number) => void;
type FnParams = Params<Fn>; // [name: string, age: number]Common errors
A frequent mistake is to use infer outside a conditional type. The compiler rejects this because infer has meaning only in the extends clause.
Another mistake is to expect a match when the pattern is too narrow. Head<[]> returns never because an empty tuple has no first element. Check the false branch when the input might not match.
Multiple infer positions can bind the same name. Each position then contributes a candidate type, and TypeScript usually combines them into a union. This can produce a wider type than you expected, so test with concrete inputs.
flowchart TD
A["Input type T"] --> B{"Does T match the pattern with infer U?"}
B -- Yes --> C["U is bound to the matched part of T"]
B -- No --> D["False branch, often never"]
C --> E["True branch uses U"]caption: How a conditional type with infer chooses its branch. alt: A flowchart. The input type T flows into a decision asking whether T matches a pattern containing infer U. A yes answer binds U to the matched part and sends control to the true branch, which uses U. A no answer goes to the false branch, which is often never.
Exercise
Exercise
Write a type FirstArg<T> that returns the type of the first parameter of a function type T. What does FirstArg<(id: number, label: string) => void> produce?
Show answer
type FirstArg<T> = T extends (arg: infer A, ...rest: any[]) => unknown ? A : never; FirstArg<(id: number, label: string) => void> is number.