Lesson 8 / 26

Function Types

Describe the shape of a function as a type, useful for callbacks.

A type for functions

A function type looks like an arrow function signature: params in, type out.

type MathOp = (a: number, b: number) => number;

const add: MathOp = (a, b) => a + b;
const multiply: MathOp = (a, b) => a * b;

Typing callbacks

Function-type parameters let TS check the callback you pass in — the wrong shape is flagged immediately.

void vs undefined

void means "the return value should be ignored"; undefined means "the value is literally undefined". Use void for callback return types.