TypeScript Essentials: Functions & Objects | Interface with Functions

TypeScript
Transcript

English (Auto-generated)

again in the typescript but this time we're gonna use the interface with the function. Uh We've already discussed about the interfaces and uh um yeah we already have had a discussion about the interfaces but we didn't apply them in the function. So first of all we're going to create the basic function, I mean a function which works in the javascript. So let's just start a function. Okay. And the function name can be anything. I'm writing some and then the the apprentices and their collaborators. So this is how we create a simple function for the javascript. The very basic method. Now let's just put some parameters like A and be okay. So these are the parameters in the in the function we are going to use but we're not using them yet so it's giving the error that is declared but its value is never read. So we need to use the values of the A. And the B. To make them working. I'm going inside the the collaboration is right here and I'm gonna create I'm gonna I'm gonna call the return okay in the return I'm gonna go with this way, sorry, this this should be this way. Okay now um it's telling a implicitly has in any type. So actually the this is the red line. Just telling me that the it has any type so I need to be very very clear about about the types of these data so I'm gonna make them number like that and absolutely the similar thing with this one too. Okay so both our numbers now what next? Um now everything is everything is fine. But uh let's say we didn't define the type of the return. So if you say void now we're going to have problems because the void means this function is going to return nothing but we return in something which is you know in v. So it's telling the, you know, the type number is not assign able to type void. So it has to be a number even we just cannot make this as you know, string because this is returning the number is straight. No question. And that question is an answer. So I'm going down and uh this is working fine and I'm just going there um and uh the function, this function is working fine. Now actually I do not need to go down, I need to go up because now I'm going to apply the interface to dysfunction. So interface, I'm gonna create the interface for the function and the interface name I'm writing is mathematics and uh you know, we know all, we already know about the basics of the interfaces but now I'm going to do something different with the interface. So now here the curly braces and I'm going inside the curly braces just like that. And now if you're creating the interface for some object, we you know what we need, we just simply need the um and we need we need to put the properties and the types but here it is not the um the, you know, the for the objects. It's for the function. So I'm just going to create the equilibrium apprentices and I'm going to call the A. N. B. Here. So A. And the data for the A. I'm mentioning the type for the A. As the number and then comma I'm writing B. And the type of the be as number. And now there is a problem because you know, the call signature which lacks return type annotation implicitly has in any return types. So I need to define the return of uh the function wherever this interface would be applied. So I'm telling it has a number. Now the interface for the function is ready. These are two parameters and the parameters types are mentioned and this is the type of the uh the return the function gonna have. So now how we can apply this interface to that function. This function, you know, this is a simple, you know, javascript and typescript mixture and the function is all is has everything. We're just making things, you know, documented by using any specific interface applying to this function. So I'm just going down and to apply the, you know, interface to the function. I need to create. I need to call the function actually. So before calling, I'm gonna call it and then let's say I'm gonna, I'm gonna tell it someone and now I'm going to apply the mathematics, this interface to this function to this variable actually. now this variable has the the the interface of the a function interface. Okay. A protocol for a function. And I'm putting equal sign, sorry, equal sign. And this some Okay, now wherever I call this, someone will actually be, you know, having this function called with this the interface of this. Let's just try. I'm calling, I'm calling someone and I'm calling a comma B. That is fine. But it is having some problems, which it should not. So I'm doing something wrong. Let me um, where I'm going wrong. Actually, the mathematics is spelling is clear. Mm hmm. Yeah, that's okay. The types are number. Mm hmm. Oh, oh my gosh. Oh my gosh, This this cannot be a this has to be the value for the A. N. D. B. I'm writing for, I am writing you know, seven. Now, that's fine. Sorry about that. Okay, now, actually the value for the A. And B is there. So if you just try to make it, you know, uh, something like that. So you're just having the problem argument of type string is not a sign. A bill to parameter number. So now this is having the method of this interface. Actually, the types of this, this interface mathematics interface. And uh, with the dysfunction. So whenever I call this someone, we're gonna have this method applied. And uh, you can say this, someone is pointing to this function with the interface of mathematics. So the mathematics interface Now, let's just say if I say string, I'm gonna have the problem because I'm telling here the, you know, this interface should have a return in string string type. But this is a problem because, you know, the number equals number is not reasonable to type, you know, mathematics. And the reason type number is not assign able to tie the strings. So the interface is the number. So ultimately, you know, we, you know this, if this is the strength, then this has to be the string and only in that case uh this can work otherwise we're going to have the problem. So this is how you can apply uh the the interface do the function. And I think it's pretty simple. Yeah. Uh sometimes we need to do some practice to make things work and make things better. So do that and until then stay tuned.
140 Views 0 Likes 0 Comments

We go over working with interfaces with functions in TypeScript.

Comment
Leave a comment (supports markdown format)