-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path103.ts
210 lines (127 loc) · 3.34 KB
/
103.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// # TS Language Extensions
// ---
// SLIDE
// ---
// ## Type vs interface
// Basically the same thing since TS 2.7
type ObjectType = {
a: string;
}
interface ObjectInterface {
a: string;
}
// Key differences:
// 1st: Types are basically aliases
type MyString = string; // you can't map a primitive to an interface
let _myString: MyString; // result: let _myString: string
// 2nd: Interfaces are open to expansion (a.k.a. declaration merging)
interface ObjectInterface {
a: string;
}
interface ObjectInterface {
b: string;
}
let _myObject: ObjectInterface;
_myObject!.a // string
_myObject!.b // string
// Side note: Notice the `!` operator, use it wisely
// This throws an error:
// type ObjectType = {
// b: string;
// }
// Rule of thumb: Use interface for defining objects, and type for complex types
// Please use interfaces when writing libraries
// ---
// SLIDE
// ---
// ## enums
enum StandardIOStream {
STDIN,
STDOUT,
STDERR,
}
StandardIOStream[0] // "STDIN"
StandardIOStream.STDIN // 0
Object.entries(StandardIOStream)
// [["0", "STDIN"], ["1", "STDOUT"], ["2", "STDERR"], ["STDIN", 0], ["STDOUT", 1], ["STDERR", 2]]
// Common use case (avoiding duplicates)
enum Direction {
UP = "UP",
DOWN = "DOWN",
LEFT = "LEFT",
RIGHT = "RIGHT",
}
Object.entries(Direction).length // 4
// ---
// SLIDE
// ---
// ## keyof, typeof
interface Person {
firstName: string;
middleName?: string;
lastName: string;
}
let _peronProp: keyof Person; // 'firstName' | 'middleName' | 'lastName'
let _john = { firstName: 'John', lastName: 'Smith' }
type Person2 = typeof _john; // { firstName: string; lastName: string; }
// ---
// SLIDE
// ---
// ## Using typeof instead of copy paste
declare function oddlySpecificDeepCopyAsync(
data: { personInfo: Partial<Person>; direction: Direction | null; /* ... */ }
): Promise<typeof data>
// ---
// SLIDE
// ---
// ## Get type from array
let _mixedArray = [1, 'something', null, [1, 2]];
type MixedArrayElement = (typeof _mixedArray)[number];
// type MixedArrayElement = string | number | number[] | null
// ---
// SLIDE
// ---
// ## Constructing objects via keyof
interface NameUseLookup {
[name: string]: {
[key in keyof Person]?: true;
}
}
let _nameUseLookup: NameUseLookup = {
'John': { firstName: true, middleName: true },
'Smith': { lastName: true }
}
// ---
// SLIDE
// ---
// ## Using them on enums
type StandardIOStreamName = keyof typeof StandardIOStream;
// "STDIN" | "STDOUT" | "STDERR"
type StandardIOStreamDict = {
[name in StandardIOStreamName]: (typeof StandardIOStream)[name]
};
const standardIOStreamDict: StandardIOStreamDict = {
STDIN: StandardIOStream.STDIN,
STDOUT: StandardIOStream.STDOUT,
STDERR: StandardIOStream.STDERR,
// STDERR: -1, -> be careful, this is valid
}
// ---
// SLIDE
// ---
// ## as const
let _john2 = { firstName: 'John', lastName: 'Smith' } as const;
type JohnSmith = typeof _john2;
// { readonly firstName: "John"; readonly lastName: "Smith"; }
// _john2.firstName = ''
// result: Cannot assign to 'firstName' because it is a read-only property.ts(2540)
// ---
// SLIDE
// ---
// ## readonly
interface ReadonlyPerson {
readonly firstName: string;
readonly middleName?: string;
readonly lastName: string;
}
type ReadonlyPerson2 = Readonly<Person>;