-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmappedTypes.ts
87 lines (67 loc) · 2.61 KB
/
mappedTypes.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Type casting (приведение к типу) -----------------------
const mistake = [] as Array<number>;
mistake.push(1);
// mistake.push('1'); // ошибка
// Быстрое приведение типов больших объектов --------------
let bigObject = {
"commit": {
"id": 12345,
"title": "JS fix"
},
"commits": [
{
"id": "12345",
"title": "JS fix"
},
{
"id": "12345",
"title": "JS fix"
}
],
"compare_timeout1": false,
"compare_timeout2": true
}
// Быстрое получение типа объекта
type TMyBigObject = typeof bigObject;
// Глобальное Readonly ------------------------------------
bigObject.compare_timeout1 = true; // можем менять
const typedBigObject: Readonly<TMyBigObject> = bigObject;
// typedBigObject.compare_timeout1 = false;
typedBigObject.commit.id = 123;
// не можем менять внешние ключи, но можем менять внутренние
// Получаем типы внешних ключей объекта -------------------
type TObjKeys = keyof TMyBigObject;
// Получаем тип внутренних ключей объекта -----------------
type TCommitType = TMyBigObject['commit'];
// Собственный Readonly -----------------------------------
type MyReadonly<T> = {
readonly [N in keyof T]: T[N];
}
// Собственный Partial ------------------------------------
type MyPartial<T> = {
[N in keyof T]?: T[N];
}
// Собственный Pick ---------------------------------------
type MyPick<T, K extends keyof T> = {
[N in K]: T[N];
}
// Рекурсивный Readonly -----------------------------------
type MyReadonlyDeep<T> = {
readonly [N in keyof T]: T[N] extends object ?
MyReadonlyDeep<T[N]> : T[N];
}
const typedBigObjectDeep: MyReadonlyDeep<TMyBigObject> = bigObject;
// typedBigObjectDeep.compare_timeout1 = false; // readonly error
// typedBigObjectDeep.commit.id = 123; // readonly error
// Убираем Readonly
type TSomeType = MyReadonlyDeep<TMyBigObject>;
// type inference
type RemoveReadonly<T> = T extends MyReadonlyDeep<infer E> ? E : T;
type TTest = RemoveReadonly<TSomeType>;
function greaterThenZero(a:number) {
return a > 0;
}
type FnReturnType<T> = T extends ((...args: any[]) => infer R) ? R : never;
type FnParameters<T> = T extends ((...args: infer R) => any) ? R : never;
type TReturnType = FnReturnType<typeof greaterThenZero>;
type TAgrsType = FnParameters<typeof greaterThenZero>;