-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLesson-07-Interface-and-Typealiases.ts
More file actions
144 lines (95 loc) · 3.12 KB
/
Lesson-07-Interface-and-Typealiases.ts
File metadata and controls
144 lines (95 loc) · 3.12 KB
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
/*
In this lesson i will tell you the about some differences between interface and type Aliases
Let me show you the code first then ill explain it in a bit
*/
interface Person{
id :number;
name :string;
age :number;
}
type person = {
id :number;
name :string;
age :number;
}
/*
now you see i have made an interface and an type alias to give shape of an object
The main difference between interface and type is
Interface only is used to structure the objects you cannot take any thing else in it
on the other hand
you can use type litreals to give more different types
for example
*/
type coin = 'Head'|'Tail';
/*
something like this unlike interface you can use type Literals for making different type of structure for your data
*/
const Person1 : Person = {
id: 1,
name: 'Harris',
age: 23
}
// console.log({Person1})
const person1 :person = {
id: 1,
name: 'Another Harris',
age: 23
}
// console.log({person1})
/*
Differences
* interface are guaranteed to be 'named' in error messages, whereas type aliases are not
it means if you are using interfaces you will like to catch the errors more fast and efficiently
on the other hand types are a little hard to debug
* interface cannot be used to rename primitives or literal types
* Type aliases cannot participate in declaration merging but interface can\
Key Points
*if you are working the API Interface are the best to pick
* you should use type alias for Union and intersection (i will explain about it in detail in the next lesson)
* but for now see the code below i will try to give some initial knowledge about it
*/
/*
UNIONS
So these are the unions where you defined multiple data types
for example if you are writing a function where you need more than data type
like the data i am going to receive in my parameter could be string or specifically what type of string
you can use literal types as Union
*/
type cards = 'Clubs'|'Diamond'|'Spades'|'Hearts'
const card : cards = 'Hearts';
// console.log(card)
/*
Intersection
For example you have defined two interfaces
and now you need to get a new collection of data where you need the structure of
both interfaces instead of creating a new interface you can use intersection for that
it would be easier and more wise this way
The example for intersection is given below
*/
interface TimeStamp {
created_at :Date,
updated_at :Date
}
type PresistedUser = Person & TimeStamp
const Presisted :PresistedUser = {
id: 1,
name: 'RisBot',
age: 23,
created_at: new Date(),
updated_at: new Date()
}
/*
Renaming Primitives
interface cannot be used to rename primitives or literal types
for example you want to use a datatype of string but creating your custom type you
can do something like this
*/
type CustomString = string;
const nice:CustomString = 'Some string '
interface some {
name:CustomString,
}
const some :some ={
name:'haris'
}
console.log(some.name)