I am fairly new to TS and learning function overloading. Somehow I am making a mistake that I can't seem to identify . The snippet is
function parseCoordinate(obj : Coordinate): Coordinate; function parseCoordinate(str : String): Coordinate; function parseCoordinate(x: number, y: number): Coordinate; function parseCoordinate(arg1: unknown, arg2: unknown): Coordinate { let coord: Coordinate = { x: 0, y: 0 }
if (typeof arg1 === 'object') { coord = { ...(arg1 as Coordinate) } } else { coord = { x: arg1 as number, y: arg1 as number } } return coord; }
I am told the titualr error for first line
CodePudding user response:
the reason is your implementation requires 2 parameters
parseCoordinate(arg1: unknown, arg2: unknown)
and 1 param overloads aren't compatible with 2 argument calls. to fix it make 2nd parameter optional by using ?
parseCoordinate(arg1: unknown, arg2?: unknown)