Function Types and Overloads
A function type is (a: number) => string. Overloads declare several call signatures before one implementation. Callbacks are typed the same way.
Example
type Op = (a: number, b: number) => number;
const add: Op = (a, b) => a + b;
function pad(x: string): string;
function pad(x: number): string;
function pad(x: string | number): string { return String(x).padStart(3, "0"); }
console.log(add(2, 3), pad(7), pad("42"));Result
5 007 042
Exercise
Arrow in a function type.
type F = (a: number) string
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.