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.

GroupGenerics
KindOperator
Strict modeNot 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 UA placeholder declaration: during extends matching, the type at that position is captured as U
T extends X ? U : YValid 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.
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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏