TypeScript Essentials: Functions & Objects | Interfaces

TypeScript
Transcript

English (Auto-generated)

uh typescript again. And in this video we're going to talk about the interfaces in the in the typescript actually the interfaces are much similar to the type, the custom types. We just have discussed about that in a previous cast. So let's just talk how the interface is actually works. And what are the benefits of using them? Interfaces are actually similar to type the custom types, but but with the, you know, a few additional things, so we're just gonna learn them. So first of all, let's just create an object. For example, I'm going to create an object like let and I'm putting the name um as a person again person and equal to and that is how you just, you know, create an object and the name. Um this is almost similarly as I've, you know, created earlier Age 28 and uh when we talk about it, the next one can be anything like, you know, height. I actually do not know my height. I mean in centimeter but I know in um in inches. So I just put that and okay, it's asking me to put the comma now that is okay. And this one Okay. And now I'm going down and the next one I'm putting here is uh is a programmer. Okay. Um this is just for putting the bully. Um so true. Okay, that's awesome. Now we've created a simple object. Now we're just going to create an interface. Now there are already two ways we've discussed discussed. The first way is to just, you know, start designing the types right here, you know, and the next other one was just to create the type, but this time I'm not going to create the time, I'm going to create the interface. Okay. There are slightly differences in the actually very slight difference in the structure of the, the, the interface and the types, we're gonna discover them. So I'm writing Kherson. Okay. And uh sorry, the name should not be string. Actually the name should be the name. Okay, that's better now. Okay, the person and here in the type, you put the equal sign in there, you don't put the equal sign. Anyway, this is the main difference but absolutely interface comes with the with a few interesting packages. Okay, here, I'm defining the types. So the first thing is I'm telling the name should be string and the age age should be uh the number and the height should be the number. Okay. And the um is a programmer should be pulling Okay, that's awesome. Now I'm just gonna simply apply this to the to hear person and just simply going to say that okay, now it is applied now, if you just try to change it this and now you're going to have the problem. So the interface is already applied here and you can see that that is perfectly applied and uh thank you, I just got my coffee here. So I'm gonna create the recording and I'm gonna have the coffee together anyway, this is the one method, you know, the the we just attach the interfaces. But what's new in the interfaces when it just simply looks like the types. So um you know, if you just simply delete that and you just save it, we are going to have a problem because in the interface we have 12344 properties with the types. Now we have to have the full properties. You can see that the property is a programmer is missing in type. So this property is a programmer has to be here. This is a limitation that when you've this is designed in a structure, you have to have them. But if you put the question mark at the end of the property. Now if you just don't put it, it is fine because this is optional. Now, because of the question mark, this became optional. So Maybe, you know, maybe you know, you have a company which have you know like 50 or let's say 80% programmers but the 20% people are not programmers. So um this probably is optional for them. But the height, the age and things are are still important for them. So I think this is an interesting thing. The question mark, I'm going to put the question mark with one more property, let's say this is an optional thing. So, but it is here. The the height is still here but the is a programmer is not there. The height is is still there and it is working fine. I think it explains well and now I think we can move on to the next thing. Let's move on. Um what is the next thing for now is um for example, you know, we, we can create one more thing, I think we never did it. Even if it's the type of, it's the interface, you can create multiple objects using this one, for example, you know the person, you can call it person one and then you can just go down and you can create person too and you just simply assign the person and equal to and then you just go down and you just create the name and now you can put jOHN so just showing you that, you know, you can use The same thing. Um it is having the okay, okay, we have to, it just tells that cannot re declare the block. It should be a person to do or something like that, whatever. I mean, it just cannot be and they both can have the same name anyway. The age now, the age of John I'm defining as 35. Okay, now I'm going down and I'm writing the height and um the as the height is optional. Now I'm not defining it and it's still, it is fine, you can see that. So you can, you know, you can use this interface for multiple objects anyway, this is one thing but I just wanted to tell you so you wouldn't create the same thing for multiple times on multiple objects. The thing is let's say you want to, you want to update the properties for this one, the person too. So you just go down and you just do one thing you write name. Sorry you're right person to that name and equal to and maybe the name is Mark not jOHN So now absolutely um this is changed. The name is not johN anymore. The name is Mark now. So I think this is pretty simple and explain well self explanatory but if you just go there and you just make it read only um read only now you because of read only you cannot modify. You know, it's just like you're creating the constant property, you know when we create and create a variable. So there is a type of the variable. You create a variable by using war by using let or by using the const so by making a property read only like this one you're actually using the Const method which in which case you just cannot modify. So this is the name is read only now this just cannot be modified. I think it is a quite interesting you know, feature. So once you explained the redone the name as read only. So you only can update the name inside the actual object but not outside it is just like maybe you know the name is Suzie but the age, you know Absolutely with every year the age changes and increases by one, the height can be, you know, the height can be changed every year or maybe you know, in decades or something like that. But um, you just cannot go with this method, like the person dot name, you know, Mark, because the name usually doesn't change, so you just can create a rule here. So this is the interface. There is one more thing in the interface which is extending the interface and we're gonna talk about that in the next cast until then stay tuned.
97 Views 0 Likes 0 Comments

We go over what exactly "interfaces" are and how to use them.

Comment
Leave a comment (supports markdown format)