-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.ts
More file actions
60 lines (44 loc) · 1.25 KB
/
utility.ts
File metadata and controls
60 lines (44 loc) · 1.25 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
interface myuser {
name: string
id: number
email?: string
phone?: string
}
// interface myuserOptionals {
// name?: string
// id?: string
// email?: string
// }
type userOptional = Partial<myuser>
type justemailandname = Pick<myuser, 'email' | 'name'>
type myuserwithoutId = Omit<myuser, 'id'>
const merge = (user: myuser, overrides: userOptional): myuser => {
return {
...user,
...overrides
}
}
const usuario: myuser = {
id: 15,
name: 'daniel',
email: 'mimail'
}
merge(usuario, {})
const mapbyId = (users: myuser[]): Record<myuser['id'], myuserwithoutId> => {
return users.reduce((a, v) => {
const { id, ...rest } = v
return {
...a,
[v.id]: rest
}
}, {})
}
console.log(mapbyId([{ id: 1, name: 'gerar', email: 'mimail' }, { id: 2, name: 'dani' }]))
const tripwithOriginRef = { originUid: '123' }
const tripOriginWhole = { origin: { uuid: '4446', city: 'bsas', state: 'arg' } }
type Trip = typeof tripwithOriginRef | typeof tripOriginWhole
type TripwithOR = Extract<Trip, typeof tripwithOriginRef>
type TripOW = Extract<Trip, typeof tripOriginWhole>
const hasOR = (trip: Trip): trip is TripOW => 'origin' in trip
const result = [tripwithOriginRef, tripOriginWhole].filter(hasOR)
console.log('result', result)