forked from mike-works/typescript-fundamentals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3-interface-type-basics.ts
128 lines (101 loc) · 2.86 KB
/
3-interface-type-basics.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
import { HasPhoneNumber, HasEmail } from "./1-basics";
//== TYPE ALIAS ==//
/**
* (1) Type aliases allow us to give a type a name
*/
// type StringOrNumber = string | number;
// // this is the ONLY time you'll see a type on the RHS of assignment
// type HasName = { name: string };
// NEW in TS 3.7: Self-referencing types!
type NumVal = 1 | 2 | 3 | NumVal[];
// == INTERFACE == //
/**
* (2) Interfaces can extend from other interfaces
*/
// export interface HasInternationalPhoneNumber extends HasPhoneNumber {
// countryCode: string;
// }
/**
* (3) they can also be used to describe call signatures
*/
// interface ContactMessenger1 {
// (contact: HasEmail | HasPhoneNumber, message: string): void;
// }
// type ContactMessenger2 = (
// contact: HasEmail | HasPhoneNumber,
// message: string
// ) => void;
// // NOTE: we don't need type annotations for contact or message
// const emailer: ContactMessenger1 = (_contact, _message) => {
// /** ... */
// };
/**
* (4) construct signatures can be described as well
*/
// interface ContactConstructor {
// new (...args: any[]): HasEmail | HasPhoneNumber;
// }
/**
* (5) index signatures describe how a type will respond to property access
*/
/**
* @example
* {
* iPhone: { areaCode: 123, num: 4567890 },
* home: { areaCode: 123, num: 8904567 },
* }
*/
// interface PhoneNumberDict {
// // arr[0], foo['myProp']
// [numberName: string]:
// | undefined
// | {
// areaCode: number;
// num: number;
// };
// }
// const phoneDict: PhoneNumberDict = {
// office: { areaCode: 321, num: 5551212 },
// home: { areaCode: 321, num: 5550010 } // try editing me
// };
// at most, a type may have one string and one number index signature
/**
* (6) they may be used in combination with other types
*/
// // augment the existing PhoneNumberDict
// // i.e., imported it from a library, adding stuff to it
// interface PhoneNumberDict {
// home: {
// /**
// * (7) interfaces are "open", meaning any declarations of the
// * - same name are merged
// */
// areaCode: number;
// num: number;
// };
// office: {
// areaCode: number;
// num: number;
// };
// }
// phoneDict.home; // definitely present
// phoneDict.office; // definitely present
// phoneDict.mobile; // MAYBE present
// == TYPE ALIASES vs INTERFACES == //
/**
* (7) Type aliases are initialized synchronously, but
* - can reference themselves
*/
// type NumberVal = 1 | 2 | 3 | NumberVal[];
/**
* (8) Interfaces are initialized lazily, so combining it
* - w/ a type alias allows for recursive types!
*/
// type StringVal = "a" | "b" | "c" | StringArr;
// // type StringArr = StringVal[];
// interface StringArr {
// // arr[0]
// [k: number]: "a" | "b" | "c" | StringVal[];
// }
// const x: StringVal = Math.random() > 0.5 ? "b" : ["a"]; // ✅ ok!
export default {};