contextual Contextual Typing

Last updated: 2026-10-22

contextual Contextual Typing

Contextual typing: when a function literal sits in a position whose type is already known (a variable annotation, an array method callback, an event handler), TS works the parameter types backwards — no annotations needed. In map((o) => o.id), o is typed not from the function itself but from the array's element type, i.e. the context.

GroupFunctions & Classes
KindSyntax
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

const orders: Order[] = [];

// o gets its contextual type Order from the array's element type
const summaries = orders.map((o) => `#${o.id}: ${o.total}`);

// A variable annotation provides context too
type ClickHandler = (event: { x: number; y: number }) => void;
const onClick: ClickHandler = (event) => console.log(event.x, event.y);

⚙️ Parameters

const x: T = ...Positions that trigger it: variable/parameter annotations, array elements, call arguments (via the formal parameter type), return expressions (against the declared return type)
(x) => ...Untyped callback parameters receive the contextual type; explicit annotations must match it or error

Returns:return expressions are context-checked too: the declared return type validates each return, catching missing branches early.

▶ Example

Edit the code below and click Try it to see the transpiled result:

Output:

#101: 250, #102: 90, #103: 480
2
click @ 12,34

(Runtime output after transpilation; a typo like o.pric inside the callback is an immediate compile error — the contextual type forbids it)

💡 Notes

ℹ️ Info Contextual typing flows types outside-in, opposite to ordinary inference (expression to variable); when both apply, the explicit annotation wins.
💡 Tip An "implicitly any" callback parameter usually means the context is missing (the array was never typed) — fix the outer type instead of annotating the callback.
⚠️ Note Callbacks like React's onChange={(e) => ...} lean on contextual types; once extracted (e.g. wrapped in useCallback) you must add the annotation yourself or parameters degrade to any.
More cases coming soon.

❓ FAQ

Q Are contextual typing and type inference the same thing?
A No. Ordinary inference goes value → type (const x = 5 is number); contextual typing goes position → value — the target type is known first and parameter types flow backwards. Callback parameters almost always come from the latter.
Q Why is my map callback parameter any?
A The array itself is untyped (e.g. from JSON.parse or a missing annotation), so there is no context to borrow from. Type the array's elements and it heals.
Q Does contextual typing let errors slip through?
A Quite the opposite — it is the basis for checking. Accessing a property outside the contextual type errors immediately (extra fields too), which is exactly how it catches typos.
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%

🙏 帮我们做得更好

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

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