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: stringFunction overloads declare multiple function signatures before the implementation.
- The implementation must be compatible with all overloads.
- Used when the return type differs depending on the 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