다음과 같이 인터페이스로도 오버로딩을 표현할 수 있습니다.
interface Add {
(x: number, y: number): number;
(x: string, y: string): string;
}
const add: Add = (x: any, y: any) => x + y;
add(1, 2); // 3
add('1', '2'); // 12
add(1, '2');
add('1', 2);
// No overload matches this call. Overload 1 of 2, '(x: number, y: number): number', gave the following error. Argument of type 'string' is not assignable to parameter of type 'number'. Overload 2 of 2, '(x: string, y: string): string', gave the following error. Argument of type 'number' is not assignable to parameter of type 'string'.