How do you write a type annotation for a function value, like a callback parameter?
Short answer
Use arrow-style syntax `(param: Type) => ReturnType` to describe a function's shape, specifying each parameter's type and the return type.
Advertisement
AModel answer
Use arrow-style syntax `(param: Type) => ReturnType` to describe a function's shape, specifying each parameter's type and the return type.
function process(callback: (value: number) => void) {
callback(42);
}
process((n) => console.log(n));Real-world example
Typing a callback parameter passed to an array method wrapper or an event handler registration function.
Likely follow-ups
- How do you write a function type as a named 'type' alias instead of inline?
Related topics