-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility2.ts
More file actions
32 lines (24 loc) · 901 Bytes
/
Copy pathutility2.ts
File metadata and controls
32 lines (24 loc) · 901 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
type Name = {
first: string
last: string
}
function addFullName(name: Name): Name & { fullname: string } {
return {
...name,
fullname: `${name.first} ${name.last}`
}
}
function permuteRows<T extends (...args: any[]) => any>(iteratorFun: T, data: Parameters<T>[0][]): ReturnType<T>[] {
return data.map(iteratorFun)
}
console.log(permuteRows(addFullName, [{ first: 'daniel', last: 'lama' }, { first: 'sofi', last: 'machano' }]))
class PersonwithFullname {
constructor(public name: Name) { }
get fullname() {
return `${this.name.first} ${this.name.last}`
}
}
function createObjects<T extends new (...args: any[]) => any>(ConstructorType: T, data: ConstructorParameters<T>[0][]): InstanceType<T>[] {
return data.map(item => new ConstructorType(item))
}
console.log(createObjects(PersonwithFullname, [{ first: 'rodrigo', last: 'lambarri' }]).map(obj => obj.fullname))