-
Notifications
You must be signed in to change notification settings - Fork 0
/
2-interfaces-and-types.ts
58 lines (44 loc) · 1.05 KB
/
2-interfaces-and-types.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
// Type Alias
// Type alias allows you to use this type by name
type NumberOrBool = number|boolean;
// Interface
// Interface can be extended by another interface
interface Email {
email: string;
}
interface Phone {
phone: string
}
interface ContactInfo extends Email, Phone {
name: string;
}
const test: ContactInfo = {
name: "Jack",
phone: "999",
email: "jack@email.com"
}
// Describe call signatures
interface Greeting {
(lang: string, name: string): string
}
type Greeting2 = (
lang: string,
name: string
) => void;
const greet1: Greeting = (lang, name) => `Hello, ${name}`;
const greet2: Greeting2 = (lang, name) => `Hello, ${name}`;
// Defined dictionary
// We need to assign undefined type for Person property,
// otherwise we'll access to item property without checking on exist this property
interface Persons {
[id: string]: undefined | {
name: string;
age: number;
}
}
const personsMap: Persons = {}
console.log(personsMap.test)
if (personsMap['Sam']) {
console.log(`Person name - ${personsMap['Sam'].name}`)
}
//