infer
infer declares a new type variable inside a conditional type's extends clause. If the match succeeds, that variable is bound to the matched piece and can be used in the true branch.
Detailed Explanation
Conditional types can test "is this a Promise?" They cannot, by themselves, name "the thing inside the Promise." infer is the binding that names it.
type AwaitedOne<T> = T extends Promise<infer U> ? U : T;
If T is Promise<string>, the pattern Promise<infer U> matches, U is string, and the result is string. If T is number, the match fails and the result is number. The built-in Awaited<T> is this idea with extra recursion for nested promises.
You can infer in any position the type grammar allows: function parameters (T extends (...args: infer P) => unknown ? P : never), return types (T extends (...args: never[]) => infer R ? R : never), array elements (T extends (infer E)[] ? E : never), tuple slots, object properties, and template literal pieces (T extends \/${infer Head}/${infer Rest}` ? ...`).
Multiple infer variables can appear in one pattern. They bind independently. T extends (infer A, infer B) is not valid on a function; you infer the whole parameter list as a tuple, then index it. Parameters<T> is that tuple.
infer only works inside the extends clause of a conditional type. It is not a generic default. It is not a runtime operator. There is no value named U. If the same conditional is distributive, infer runs once per union member, and the inferred types are unioned — or, in some parameter positions, intersected — according to variance. Function parameter inference, in particular, produces an intersection when distributing, which is why Parameters<Fn1 | Fn2> can look stricter than you expected.
A failed match just takes the false branch. There is no exception. If you write a pattern that is slightly wrong — Promise<infer U> against PromiseLike<U> — you get the false branch and a type that looks like you forgot to unwrap.
The practical rule: if you are about to write a chain of indexed accesses that might miss, write a conditional with infer that names the piece you want and provides a fallback. That is how you type array[0] as the element type, how you peel wrappers, and how you parse string literal types.
Code Examples
1. Unwrap a Promise, leave other types alone.
// 1. Unwrap a Promise, leave other types alone.
type UnwrapPromise<T> = T extends Promise<infer U> ? U : T;
type A = UnwrapPromise<Promise<number>>; // number
type B = UnwrapPromise<string>; // string2. Function parameters and return type — the idea behind Parameters and ReturnType.
// 2. Function parameters and return type — the idea behind Parameters and ReturnType.
type Args<T> = T extends (...args: infer P) => unknown ? P : never;
type Ret<T> = T extends (...args: never[]) => infer R ? R : never;
function charge(cents: number, currency: string): boolean {
return cents > 0;
}
type ChargeArgs = Args<typeof charge>; // [cents: number, currency: string]
type ChargeRet = Ret<typeof charge>; // boolean3. Infer inside a template literal type.
// 3. Infer inside a template literal type.
type PathHead<S extends string> =
S extends `/${infer Head}/${string}` ? Head :
S extends `/${infer Head}` ? Head :
never;
type H = PathHead<"/users/42">; // "users"Common Gotchas
`infer` is illegal outside a conditional `extends`
`type X = infer U` does not parse. Nest it in `T extends Pattern<infer U> ? U : never`.
Distributive inference over a union of functions intersects parameters
two call signatures combined that way can become `never` for a parameter. Treat function unions with care, or wrap to disable distribution.
Infer the whole tuple, then index
do not try to `infer` two function parameters as two variables in one `(infer A, infer B)` pattern.
A slightly wrong pattern silently takes the false branch
if an unwrap utility returns the original type, the `extends` pattern did not match. Check wrappers like `Promise` vs `PromiseLike`.