-
Notifications
You must be signed in to change notification settings - Fork 0
/
video10.js
55 lines (51 loc) · 1.09 KB
/
video10.js
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
// @flow
// ========================================= Generic. Базовый пример
type Response<T> = {
success: boolean,
entries: Array<T>,
meta: {
total: number,
limit: number,
offset: number
}
}
type User = {
name: string
}
const data: Response<User> = {
success: true,
entries: [{ name: 'Vasya' }],
meta: {
total: 10,
limit: 2,
offset: 0
}
}
function withStatus<T>(a: T): {| data: T, status: boolean |} {
return {
data: a,
status: true
}
}
const r = withStatus(data)
// ========================================= Generic функции в функции
function extract<I, O>(func: (obj: I) => O, r: I): O {
return func(r)
}
function cb(x: number) {
return 'foo'
}
const s = extract(cb, 5)
// ========================================== Generic в классе
class Wrapper<O> {
data: O
}
const wr: Wrapper<{ name: string }> = new Wrapper()
wr.data.name.includes('-')
// ===================================== Object map
type ObjectMap<T> = {
[key: string]: T
}
const f: ObjectMap<number> = {}
f.foo = 1 // ok
// f.bar = 'baz' => error