(arg) => R Arrow Function Types

Last updated: 2026-10-22

(arg) => R Arrow Function Types

Arrow functions have built-in annotation syntax: a colon after parameters, and the expression is the return value. Declare the variable's function type and the body's parameters/returns link automatically — the recommended style for one-off callbacks like comparators.

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 byPriceAsc: (a: Product, b: Product) => number = (a, b) => a.price - b.price;

// Generic arrow (.tsx files need <T,> to avoid JSX ambiguity)
const identity = <T,>(v: T): T => v;

⚙️ Parameters

(a: Product, b: Product) => numberThe variable's function type: a/b in the implementation get their types automatically
<T,>(v: T): TGeneric arrow: type parameters precede the parameter list; the trailing comma disambiguates in .tsx

Returns:Expression bodies return the expression's type; block bodies use return, with an optional explicit return annotation.

▶ Example

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

Output:

Mouse:89 | Keyboard:199 | Monitor:899

(Runtime output after transpilation; a boolean comparator (a.price < b.price) is a type error — sort expects number)

💡 Notes

ℹ️ Info Arrow functions have no own this: callbacks and handlers use the surrounding this — the key runtime difference from ordinary functions.
⚠️ Note Wrap object literals in an expression body: () => ({ key: v }). Writing () => { key: v } parses as a block and returns undefined.
💡 Tip Return a.price - b.price (negative/zero/positive) for ascending order; never booleans — subtracting booleans works by accident and is a type error anyway.
More cases coming soon.

📚 Related tutorials

❓ FAQ

Q Do arrow and ordinary functions type differently?
A Identical at the type level: (params) => returnType either way. Differences are runtime semantics (this, arguments, constructibility).
Q Why does <T>(v: T) fail in .tsx files?
A The generic angle brackets collide with JSX tags. Write <T,> (trailing comma) or <T extends unknown> to disambiguate.
Q When should an arrow function variable be annotated?
A When assigned to a variable, stored in an object, or exported — the annotation feeds contextual typing into the body. Inline positions with known parameter types (sort(byPriceAsc)) need none.
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%

🙏 帮我们做得更好

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

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