TypeScript Essentials: Functions & Objects | Functions


English (Auto-generated)

Hello and welcome back. So in this video we're gonna talk about um the functions in the typescript. So actually the functions of the type type scripts are quite simple and quite easy to understand. All you need is to you know create a function just like the just like you do in the javascript for example that's how you can you create a function in the javascript. So function um Okay that is good. Now now you need to name the function for example you can call it some or anything I mean if you want to some something so you know you you know you just created a function now you're just gonna go here and you can do the console log Okay? And uh you can do something like you know um You know 5-plus 8 and you're going to get the value of this console log. So um what you do here is um you can receive some values from um as a parameter. So you just write, you know A. And B. This is how you just create a simple function but you're you can see that um we are having some problems but the problem is here is the bees declared but its value is never read so now instead of five plus eight because these are the you know handwritten or you can say the you know written code uh static value you can say so I'm going to make it A. And D. B. Now this is better now but still there is a problem A implicitly has any type. Remember when we were talking about the types, we talked about the type which is any. So the A. Is any type. So you know you just need to be here A. A plus B. And uh it will work. So there is one more way to create a function for example a function um someone and uh ex n Y. And then you just go inside and instead of fighting a control log, I mean avoid function which returns nothing. I'm now creating a returning function which is going to return something so it's gonna return X plus y. Okay that is you know, simple and understandable. This function is going to have a value when we call this function and this function is just gonna show or print something on the screen but it's not going to return anything. Both of have both of them has the, you know the red lines on their parameters. Under their parameters actually. So what is the solution actually? The type is any means uh you know these are the, you know the dynamic type parameters for now. So let's just say whenever you call the function for example you're calling the someone so you can put the value like you know, a or I mean anything, you know like you can write hello comma world okay. And that is fine. However, it it shouldn't be fine but it is so I mean you know this uh I mean we are doing the, you know you're doing this some so this has to be the entire job not the you know the the the string. So you just simply can go there or there and you can make it a number now. You it is right now because it's not the any but it's specifically the number type. So you can see that here. The hello is also is now underlined because you can see that the argument of type string is not assign able to parameter of type number. So Now this must be a number for example, you know, it can be anything like nine or whatever you want to. And similarly if you go there for now it is any which is not recommended but you can make it in a string, you can make it in a string and it is fine. However the formula what we're returning would you know cause issues. So this should not be a string logically, it has to be a number because what we're doing after that is doing the some. So I'm changing it to number but According to the you know the typescript rules that was okay but programmatically absolutely it was causing the problem. Now we do not need this string and we're going to have something like you know 18. Now this is absolutely fine but this one is having the problem and the solution for this one is absolutely the similar. So you can you know, you can make it the string, you can mix anything. Uh but I'm making it a number because this is ultimately the plus sign. We are we are summing up them but you know sometimes we we can concatenate these strings too. So this sign is also can also do the concatenation between two strings, so that is also acceptable. Now just go there and add another value which is a a number number. And here you can put a string or you can put number or whatever you want. I mean I I just put number and then I'm adding the um the pipe and in the pipe or after the pipe um I can add something like that. So it becomes the union. Now you understand that that's the point. Okay, now this becomes the union. Remember that we just talked about the unions where this be now can have to kind of values, it cannot be a bullion, but it can be a number or a string. But this creates a problem here because you know, the plus operator cannot be applied to the types number, any string number. So it has to be, you know, this one has to be something which can be applied for example, it you know, this would work fine. So this is a simple rule anyway. Now, you know, you're not returning anything. So there is also a way to behave, you just simply can put void here. Now this defines the type of the return but here we are returning something but we also can define what we should expect when we are returning anything from this function. So after the, you know the parentheses, I can add something like that and we can write string. So now we we would get the return as a string but absolutely the type number is not a sign able to type string. So now you can see that how amazing the typescript is. It is literally telling us that these are the number of values, I mean these gonna have the values in numbers so we are doing something wrong which needs to be corrected. So this now has to be the number and now absolutely this whole function is fine and you're gonna have the proper return and I think this makes sense. Now this is the this is returning the number anyway if you want something, you know just the str or str one or whatever. So in that case um you know I'm writing C the this is the way you do things. So here in the typescript you just simply need to write this string string Now absolutely they are gonna have some string stuff. I'm going down and I'm returning I'm actually making a return function. So this function is going to return something. See plus the will be returned. Okay, now the type has, I cannot be the number because the number, you know, you can see that the number this spring cup is not assign able to type number. So it has to be string because these two variables or parameters are actually have the time, which is a string. So now I think things makes sense now and now we can move on to something else because I I'm hoping that you now understand the, how the functions can be upgraded into the better version in the typescript before moving along. We need to see the results. So here this is what we just did and this is what we've got, you know, simple, simple as that. And I think this is very much understandable, I mean, you know, but this is, you know, you can say that this similar code. I mean this is exactly the same but this is much documented and very well designed and very well coded. So hopefully later you're not gonna have any issue in writing the, you know, in putting the values in when whenever you call the function because you defined it that it has to be this ring and it has to be the number and this is gonna return nothing because it's void. So if this we are asking this function to return anything, we are wrong. So it, you know, it explains well, okay then in the next video we're absolutely going to talk about something else until then stay tuned
100 Views 0 Likes 0 Comments

We'll go over working with functions in TypeScript.

Leave a comment (supports markdown format)