-
Notifications
You must be signed in to change notification settings - Fork 0
/
105.ts
164 lines (84 loc) · 2.8 KB
/
105.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// # Conditional Types
// ---
// SLIDE
// ---
// ## Syntax
// SomeType extends OtherType ? TrueType : FalseType;
type FunctionIsObject = Function extends object ? true : false;
// type FunctionIsAnObject = true
// ---
// SLIDE
// ---
// ## Used with generics
type ReadonlyIfObject<T> = T extends object ? Readonly<T> : T;
interface MyObject {
a: number;
}
let _readonlyMyObject: ReadonlyIfObject<MyObject> = { a: 1 };
// _readonlyMyObject.a = 2;
// Cannot assign to 'a' because it is a read-only property.ts(2540)
let _notReadonlyNumber: ReadonlyIfObject<number> = 1;
// let _notReadonlyNumber: number
_notReadonlyNumber = 2;
// ---
// SLIDE
// ---
// ## infer
let _mixedArray2 = ['something', null, [1, 2]];
type ElementOf<T> = T extends Array<infer E> ? E : T;
type MixedArray2Element = ElementOf<typeof _mixedArray2>;
// type MixedArray2Element = string | number[] | null
type SomeNumber = ElementOf<number>;
// type SomeNumber = number
// N.B.: `infer` always goes between `extends` and `?`
// ---
// SLIDE
// ---
// ## recursive
type ElementOfRecursive<T> = T extends Array<infer E> ? ElementOfRecursive<E> : T;
type MixedArray2ElementRecursive = ElementOfRecursive<typeof _mixedArray2>;
// type MixedArray2ElementRecursive = string | number | null
// NOTE: Some restrictions if < 4.1
// FUN FACT: Recursive type aliases needed to be "hacked" before 3.7 [Nov 2019]
// 3.7 is the one that brought optional chaining and nullish operators
// ---
// SLIDE
// ---
// ## never
type OnlyElementOf<T> = T extends Array<infer E> ? E : never;
type SomeNumber2 = OnlyElementOf<number>;
// type SomeNumber2 = never
declare function elementAt<T>(array: T, index: number): OnlyElementOf<T>;
let _elementAtArray = elementAt(_mixedArray2, 0);
// let _elementAtArray: string | number[] | null
let _elementAtObject = elementAt({ a: 1 }, 0);
// let _elementAtObject: never
// ---
// SLIDE
// ---
// ## Filtering with never
type PrimitiveOnlyElementOf<T> = T extends Array<infer E>
? (E extends object ? never : E)
: never;
type MixedArray2PrimitiveElement = PrimitiveOnlyElementOf<typeof _mixedArray2>;
// type MixedArray2PrimitiveElement = string | null
// ---
// SLIDE
// ---
// ## A useful example
type GetReturnType<T> = T extends (...args: any) => infer R ? R : never;
type DateNowReturnType = GetReturnType<typeof Date.now>;
// type DateNowReturnType = number
let _mixedArray2ElementAt = (index: number) => elementAt(_mixedArray2, index);
type MixedArray2ElementAtResult = GetReturnType<typeof _mixedArray2ElementAt>;
// type MixedArray2ElementAtResult = string | number[] | null
// ---
// SLIDE
// ---
// ## This Already exists
type DateNowReturnType2 = ReturnType<typeof Date.now>
// type DateNowReturnType2 = number
// see 006 for exact definition
// ---
// NEXT
// ---