Function Types
What
Function types allow you to treat functions as first-class citizens by defining their shape (parameters and return type) as a standalone type.
How
You define a function type using the arrow syntax (params) => returnType:
// Define a variable that must hold a specific function shape
let mathOp: (a: number, b: number) => number;
mathOp = (x, y) => x + y; // OK
mathOp = (x, y) => x * y; // OK
Why
Defining function types is essential for higher-order functions (functions that take other functions as arguments). It ensures that any callback passed to a function matches the expected signature, preventing “undefined is not a function” errors at runtime.
Summary
Function types allow you to abstract logic and pass functions around your application with the same safety as primitive values.