오버로딩할 때 주의할 점이 있습니다. 너무 지나치게 오버로딩을 활용하면 안 됩니다. 다음은 애초에 오버로딩할 필요가 없는데 오버로딩했다가 문제가 되는 경우입니다.
function a(param: string): void
function a(param: number): void
function a(param: string | number) {}
function errorA(param: string | number) {
a(param);
}
// No overload matches this call. Overload 1 of 2, '(param: string): void', gave the following error. Argument of type 'string | number' is not assignable to parameter of type 'string'. Type 'number' is not assignable to type 'string'. Overload 2 of 2, '(param: number): void', gave the following error. Argument of type 'string | number' is not assignable to parameter of type 'number'. Type 'string' is not assignable to type 'number'.
function b(p1: string): void
function b(p1: string, p2: number): void
function b(p1: string, p2?: number) {}
function errorB(p1: string, p2: number | undefined) {
b(p1, p2);
}
// Argument of type 'number | undefined' is not assignable to parameter of type 'number'. Type 'undefined' is not assignable to type 'number'.