Alright so in this video we're going to talk about interfaces 1
that are a way to create your own types, so that's 2
something that we do a lot with typescript so that is a very 3
important concept and you'll see that it's actually pretty 4
simple and you also see that there are actually two ways of 5
creating your own types we have interfaces and we have types 6
but we're going to talk about types later for now we're just 7
going to use interfaces and I personally use almost only 8
interfaces. So when you want to create your own type you can do 9
it like this, use the keyword interface and then you have to 10
use the Pascal case for your interface so you have to start 11
with an uppercase and for example I could create 'Person' 12
and then you append curly braces like this and then inside you 13
can declare your type for example I could use what I have 14
here so I could say that a person is a name a job and an age 15
and now I could just replace what I've here by my person so 16
that's what I'm going to do so now as you can see it's easier 17
to read we understand that it's an array of person and then I'm 18
going to replace that here as well. And as you can see if I 19
remove it actually by default it's supposed to be a person 20
yes because this is an array of person so in each iteration I 21
have one person so that's perfect but you can write it 22
like this and that's pretty much it that's how you're going 23
to create an interface and for now we just created a string 24
numbers, so I could create a isHappy that could be a boolean 25
and as soon as I do that as you can see now it's not working 26
anymore and if we read the error it's saying that the 27
property is happy is missing in the type ,this, so name, job 28
age but is required in the type person so you understand that 29
you have to add isHappy and when I press control space actually 30
it's offered so I'm going to say true for example but I 31
could also make it optional this way I don't have to 32
provide it everywhere and I could also have, I don't know, a 33
friend list that could be an array of person okay that is 34
going to be non required but here I could add a friend list 35
that could be an array of person and then I can just add 36
a new person here 37
Toby and I'm going to start here, okay? So As you can see 38
you can use interfaces into other interfaces, there is no 39
problem with that. And for example I could also have a 40
function in my interface okay I could have a "sayHi" function so 41
to type a function you're going to do it like this you're going 42
to say that it's a function that returns and then you're 43
going to say what is the type so for example I could say that 44
it's going to return void that is a new type and it's when you 45
don't return anything you're just performing an action in 46
the function okay and now here in my person here I can have a 47
sayHi and it's going to be a function that does something and 48
that's it okay so for example I could write I am Alice but as 49
you can see the sayHi function is not optional so it's 50
expected in the friend list it's expected for each person 51
so I'm going to make it optional as well and now if I 52
want to for example call this function I can just choose my 53
persons here then I'm going to access the first one and just 54
call sayHi and as you can see it's going to say hey sayHi is 55
probably undefined so you have to make sure that it's not, 56
before calling it so you have to perform an if statement so 57
to do that you can use again the question mark so it's a bit 58
odd but you can do it right here and then use the dot, so 59
right before the dot right before calling the function 60
you're just going to test if it's defined or not and I'm 61
going save and ta-da "I am Alice" so it's working and you 62
could even send parameters to your function I could have I 63
don't know a color that is a string and you can do whatever 64
you want with it okay so now I'm forced to send a string 65
here so red for example and you can use it if you want from the 66
inside so "I am Alice ! my favorite color is " and now you 67
can receive the color and as you can see typescript is 68
quite flexible because even if I don't have all the parameters 69
it's not going to throw an error that is the behavior of 70
typescript it's very specific with functions , if you say 71
that you want this function to be like this you don't actually 72
have to use all the parameters okay but if you receive them 73
for example if I do color no if I for example do I don't know 74
uh country and make it a boolean it's not going to work 75
okay it's going to say no, if you want a parameter it has to 76
be a string So, it has to be a string like this and this is 77
going to work so this add more flexibility if you don't want 78
to use the parameter okay and my favorite color is color I'm 79
going to refresh. And yet my favorite color is red. Okay? So 80
that is pretty much it with interfaces. In the next video 81
we're going to talk about classes because this is an 82
important concept as well and after that we're going to do an 83
exercise to practice everything that we've learned from now. 84
Okay? So see you on the next one.