-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoverloads.ts
More file actions
40 lines (33 loc) · 867 Bytes
/
overloads.ts
File metadata and controls
40 lines (33 loc) · 867 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
interface Coord {
x: number
y: number
}
function parseObj (obj: Coord) : Coord {
return {...obj}
}
function parseNum (x : number, y: number) : Coord {
return {x, y}
}
function parseCoord(a: string) : Coord
function parseCoord(obj: Coord) : Coord;
function parseCoord(x: number, y: number) : Coord;
function parseCoord(arg1 : unknown, arg2?: unknown) : Coord {
let coord: Coord = { x: 0, y:0}
if(typeof arg1 === 'string'){
(arg1 as string).split(',').forEach(str => {
let [key, value] = str.split(':')
coord[key as 'x' | 'y'] = Number(value)
})
} else if(typeof arg1 === 'object'){
coord = {...(arg1 as Coord)}
} else {
coord = {
x: arg1 as number,
y: arg2 as number
}
}
return coord
}
console.log(parseCoord(10,20))
console.log(parseCoord({x:1, y:2}))
console.log(parseCoord('x:15,y:15'))