-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerics.ts
More file actions
54 lines (43 loc) · 880 Bytes
/
Copy pathgenerics.ts
File metadata and controls
54 lines (43 loc) · 880 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
function simpleState<Type>(initial:Type) : [() => Type, (s:Type) => void]{
let value : Type = initial
return [
() => value,
(s:Type) => { value = s}
]
}
const [value, setValue] = simpleState<string | null>(null)
interface Rank <RankItem>{
item: RankItem
ranking: number
}
function ranker<RankItem>( items : RankItem[], rank: (v : RankItem) => number ) : RankItem[] {
const ranks : Rank<RankItem>[] = items.map(item => {
return {
item,
ranking: rank(item)
}
})
ranks.sort((a,b) => b.ranking - a.ranking)
console.log('ranks', ranks)
return ranks.map(rank => rank.item)
}
interface Pokemon {
name: string
hp: number
}
const pokemon : Pokemon[] = [
{
name: 'bulba',
hp: 20
},
{
name: 'mega',
hp: 15
},
{
name: 'tiny',
hp: 7
}
]
const rk = ranker(pokemon, ({hp}) => hp )
console.log(rk)