Function overloads khai báo nhiều function signatures trước implementation.
- Implementation phải compatible với tất cả overloads.
- Dùng khi return type khác nhau tùy input type.
typescript
function parse(x: string): number;
function parse(x: number): string;
function parse(x: any): any {
if (typeof x === 'string') return parseInt(x, 10);
return String(x);
}
const n = parse('42'); // n: number
const s = parse(42); // s: string