-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerics.ts
62 lines (52 loc) · 1.22 KB
/
generics.ts
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
60
61
62
// Replace String with a genereic from last example from tuples
function simpleState<T>(
initalValue: T
): [() => T, (value: T) => void]{
let state: T = initalValue;
return [
() => state,
(value: T) => {
state = value
}
]
};
// Will accecpt multiple types as long as T is all the same type in implementation
const [st1getter, st1setter] = simpleState(1)
console.log(st1getter())
st1setter(62)
console.log(st1getter())
// Overriding inferred generic type
const [st2getter, st2setter] = simpleState<string | null >(null)
console.log(st2getter())
st2setter('str')
console.log(st2getter())
// Generic interface example
interface Rank<RankItem> {
item: RankItem;
rank: number;
}
function ranker<RankItem>(items: RankItem[], rank: (v: RankItem) => number): RankItem[] {
const ranks: Rank<RankItem>[] = items.map((item => ({
item,
rank: rank(item)
})
))
ranks.sort((a,b) => a.rank - b.rank)
return ranks.map((rank) => rank.item)
}
interface Pokemon {
name: string;
hitPoints: number;
}
const pokemon: Pokemon[] = [
{
name: 'bulba',
hitPoints: 20,
},
{
name: 'charmander',
hitPoints: 15,
}
];
const ranks = ranker(pokemon, ({hitPoints}) => hitPoints)
console.log(ranks)