Dhananjay Patel Logo
  1. Home
  2. / Blog
  3. / Typescript Basic
  4. / Lessons
  5. / 6

Next

index.ts
interface Coordinate {
x: number,
y: number
}
function parseCoordinate(obj: Coordinate): Coordinate
function parseCoordinate(x: number, y: number): Coordinate
function 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))

Next