interface Coordinate { x: number, y: number}
function parseCoordinate(obj: Coordinate): Coordinatefunction parseCoordinate(x: number, y: number): Coordinatefunction parseCoordinate(arg1: unknown, arg2?: unknown): Coordinate { let result: Coordinate = { x: 0, y: 0 }
if(typeof arg1 === 'object') { result = { ...(arg1 as Coordinate) } } else { result = { x: arg1 as number, y: arg2 as number } }
return result}
console.log(parseCoordinate({x: 10, y: 15}))console.log(parseCoordinate(40, 50))
- Home
- / Blog
- / Typescript Basic
- / Lessons
- / 6
Next
Next