(arg: T) => R Function Type Expressions

Last updated: 2026-10-22

(arg: T) => R Function Type Expressions

A function type expression describes the shape of a class of functions: parameters, =>, return type ((value: string) => string | null). Give it a type alias and callbacks, validators and middleware — anything function-valued — get a reusable name.

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

type Validator = (value: string) => string | null;
type Comparator<T> = (a: T, b: T) => number;

const required: Validator = (v) => (v.length > 0 ? null : "Required");

⚙️ Parameters

(value: string)Parameter list: names are documentation; matching is positional
=> string | nullReturn type: read => as "returns"; here null conventionally means "no error"

Returns:Function types are pure type descriptions with no runtime code; assigning to such a variable triggers item-by-item compatibility checks.

▶ Example

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

Output:

["Required","Invalid email"]
[]

(Runtime output after transpilation; pushing a function whose parameter is not string into rules is a compile error)

💡 Notes

ℹ️ Info Parameter names in a type are hints only: (v: string) => R and (value: string) => R are the same type; position and types do the matching.
💡 Tip Any recurring function shape (handlers, loaders, middleware) deserves a type alias — signature changes then touch one place.
⚠️ Note With string | null returns, callers receive a possibly-null value and must check first; make the "null means no error" convention explicit in docs.
More cases coming soon.

📚 Related tutorials

❓ FAQ

Q Function type expression vs callable object type?
A (v: string) => R describes only the call; interface { (v: string): R; version: string } also carries properties (a callable object). Plain callbacks need just the expression.
Q Do callback parameters still need annotations?
A No. In const v: Validator = (v) => ..., parameter types come from the Validator context (contextual typing); only standalone function literals need annotations.
Q What's special about () => void?
A A () => void slot accepts functions that do return values — the value is simply ignored. That's why event systems accept almost any callback.
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%

🙏 帮我们做得更好

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

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