infer The infer Keyword
Last updated: 2026-10-22
infer The infer Keyword
infer captures a type inside a conditional's true branch: T extends Array<infer U> ? U : never means "if T is an array, extract its element type as U". It upgrades matching from "match and discard" to "match and extract" — the core of utilities like ReturnType.
| Group | Generics |
|---|---|
| Kind | Operator |
| Strict mode | Not required |
ℹ️ Info The editor transpiles TypeScript to JavaScript and runs it (no type checking); see each example's Output for type-check results.
📝 Syntax
type ItemOf<T> = T extends Array<infer U> ? U : never;
type A = ItemOf<string[]>; // string
type B = ItemOf<number[]>; // number
// infer may appear anywhere in the pattern
type FirstArg<T> = T extends (first: infer A, ...rest: any[]) => any ? A : never;
⚙️ Parameters
| infer U | A placeholder declaration: during extends matching, the type at that position is captured as U |
|---|---|
| T extends X ? U : Y | Valid only inside the current conditional's true branch; invisible outside it |
Returns:infer is not a type but a binding made during matching; the conditional's result is computed from what was captured.
▶ Example
Edit the code below and click Try it to see the transpiled result:
Output:
11 A
(Runtime output after transpilation; n and s are number and string at compile time — infer extracted the element types)
💡 Notes
ℹ️ Info infer can sit anywhere in the extends pattern: Promise<infer U> grabs the resolve type, [any, infer R] the second tuple slot, (x: infer P) => any the parameter — position is meaning.
💡 Tip Repeated same-named infer triggers union-of-candidates: [infer T, infer T] matches [number, number] as number but fails on [string, number] — a natural consistency check.
⚠️ Note infer is only legal inside a conditional's extends clause — a bare type U = infer X is a syntax error, and U cannot be referenced in the false branch.
🏷️ Related Generics
💡 Related cases
More cases coming soon.
📚 Related tutorials
❓ FAQ
Q infer vs a generic parameter <T>?
A Generic parameters are given by the caller; infer extracts from an existing type. One declares input, the other mines information — ReturnType<T> infers a return from a function type.
Q What happens to U when matching fails?
A No match, no binding — control simply falls to the false branch, which typically yields never or unknown as the fallback.
Q Which built-in utilities use infer?
A ReturnType, Parameters, ConstructorParameters, Awaited and more — all extends + infer combos; learn infer and most utility-type source becomes readable.