(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.
| Group | Functions & Classes |
|---|---|
| Kind | Syntax |
| 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 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 | null | Return 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.
🏷️ Related Functions & Classes
💡 Related cases
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.