Infer
What
The infer keyword is used within a conditional type to “capture” a type variable from a matching pattern.
How
You use infer followed by a variable name in the part of the conditional you are trying to match:
type GetReturnType<T> = T extends (...args: any[]) => infer R ? R : never;
function greet() { return "Hello!"; }
type Result = GetReturnType<typeof greet>; // string
Why
infer allows you to extract information from complex structures that you don’t control. For example, you can extract the return type of a function, the type of items in an array, or the resolved value of a Promise, all without needing to know those types upfront.
Summary
infer is a powerful tool for pulling internal type information out of larger, more complex structures.