Arrays and Tuple

Transcript

English (Auto-generated)

So in this video we're gonna talk about the the arrays. And the another thing is to pull so again we're talking about the data types but this time we're gonna cover um the arrays. So what is the tuple? Actually, tuple is uh is a kind of thing, just like array but with with you know different manner if you're coming from the python background then you may know about the tuple very well but you know, not were not only covering the people who knows python already, so we will be defining the two. Tuple itself. So first of all, we're going to go with the the simple arrays, so how to create an array in the typescript? Um there are two ways to create an ira in typescript. So actually three ways that you know, one array, one kind of way is to define the uh the the array with the same data, another one with the with the multiple multiple type of data. So first and the third one is a triple by the way. So let's just talk about the first method, we're going to just create one. So we're going with the or I think we already have a discussion on this. So I'm just gonna start with the letter. Now we are are one and then you know defining the type and it can be like you know, string but because you know, it is an array, so you have to define this thing to hear. So. Absolutely. Now this is only going to be an array by the way. So here and then whatever you want to put inside, you can put for example you can leave it, leave it empty or you can put anything if you want to. So I'm putting something like, you know, I'm writing first hurry here. Now that is totally fine if I go there and if I, if I write the TSC, I'm gonna have the, you know what you can expect, what I'm going to get. So I'm just simply, you know, doing that and I'm going with the the um the the index. So Jason, I'm gonna have the simple array. So you know that but the problem comes when I put a comma and I write something like you know um five and I also write through, I mean the bullying is also here. So absolutely, we're having the problems and now if I simply go there and if I try PSC and then you are gonna you're gonna see that okay, we got two errors in this program. You can see that the errors are clearly mentioned here. This one is the error and that one is the error. So the number is not assigned to a type string. The bullying is not assign able to type. Strings of both errors are understandable but here we got over array. Um and why we got it because you know, it is possible in javascript. However it's not possible in typescript. So that's a quite interesting factor but maybe you want some array which can consist of multiple data types. So there is a way because it's an array so they had to make, you know, exemptions. So the way is pretty simple. I'm creating another array and I'm writing it as you know, let A R R two and then here defining the types now to define the time I'm gonna I'm gonna have to create it this way, the parentheses and then the string the second better type, which can be anything. So I have to add a pipe between and then the, you know, this can be the number. Okay, so this already and if you want, if you have another one you can put the type here by adding in the space but not now, so equals before equal sign. I have to define that. This is in a race. So I'm gonna put this the square brackets to define the type of the variable that it is an array and then, you know, the now I can now put the, you know, the number for example the before and that is fine. And now it is not giving me any error even though I mentioned that this can have a strength but we don't have a string right now. So that is also ok because OK, this theory can you can have the string even though we don't have now. So that works, but I'm adding some strings like you know, some spring. Okay, that's a kind of literal play anyway, so that is fine and you're not gonna have any error. You can see that no red line or something like that, but if I had a comma and if I now put the true now we are absolutely gonna have the error because that is something not acceptable. It's false because the bullying is not assign able to type string. So now I think, you know, this is the array with multiple data types. So I think that is totally cleared to you. Let us see what we are going to get in the, you know in the javascript. So I'm just running the the PSC compile it and I'm going there and you can see that it is exactly the same as double one with the two data types because in javascript you can put multiple data types in the same array. So that is acceptable in javascript. And here in typescript we have to define it this way. So I think you're, the more you're digging into the typescript, you're more understanding it that you know, the typescript is actually um is the way to define something like you can see that this training the number. So actually it is defining um um it is making the code more readable, it is making the code uh you know bug minimized and things like that. So you can code it freely because you're sure you're not gonna have any problem. Okay, so that's understandable, The next thing we're gonna do is the tuple. So what is a tuple tuple is something, you know, quite interesting. So I'm gonna I'm gonna, you know, I'm gonna create the tuple. Mm hmm. Right here. And I'm writing mm hmm. Let okay. And I'm writing, you know, TUP one. Okay, now I'm defining the type. Okay, now I'm first writing S T R I N G adding a comma. Okay, this is a different thing. I'm writing number. Okay. And I'm just going there and I'm going to define you what I exactly did. Um I do not need to define, you know, the empty curly the square brackets, you know, to define the type because already we have the square brackets. So I'm going there Now. The thing is what we did is um because it is the tuple the two people in the tuple, we define the types of the properties. I mean, you know, we first define the string and then the number but here in the values we first put the number, not the string. This here the string was the first one. So here we just simply radley mentioned that these two types can be inside an array inside this array and we also can remove one data type in. It would still work because, you know, even though we said that this array can contain both, but we have we just put one and we can put more and we absolutely can put more data, for example adding a comma and adding you know um Sorry? Yeah, like that and I'm writing something like okay anything I mean you know, so this is totally the choice 677. So you can see that we just defined that this array can contain both of the data but here we are defining the position of the values. So the first data would have to be a string. The first property has to be string and even for now we don't have any property because it's it's a tuple because of this method. It is the tuple so it is giving us an error and if I just you know, however the mouse. So it's telling type this is not a sign able to type this one. Okay. It's literally telling that the empty, I mean this one is empty, you know, empty array. So the Mtr is not assign able to type the string and the numbers. So we need we have to have both. You can also say see that these sources source has zero elements but target Requested two. So it is literally forcing us to put both of the data and that is an amazing amazing thing that people is an amazing thing. For example you're creating kind of in the user data. So maybe you need the user password, you need the user user email, the user name and you know, maybe some lucky number for the user, I mean anything. So you absolutely can define the position. And if some data is missing the tuple gonna have the problem because it is going to receive the list data. So I think that's nice. That's a beautiful uh increment ation in the javascript. First of all, the summer spring, I mean you know um some data, anything I can write. No problem. Some data and I'm saving it and I'm going to add the comma. Now the next one has to be a number to remove this red line. It says that source has one element but still target requires two. I'm writing six. Now there is no error and everything is fine. So that is two people and I think that's pretty much explained. We'll explain itself. Okay if I write five here, I'm going to have the problem because uh you know, source has three numbers and the target allows only two. So this is how strict the tuple is. So if you have another number so you need to make the position in the type because you're defining the type of the property. So if I write a string here, it is still the problem because now this is the area is not here. The area is not here. But the error is here because um the type number is not a sustainable to type string because here uh the tuple is expecting us to put the some string because this is the string. Now that's quite interesting. But if I make the type number. So absolutely, this is the number on the third level and here the third data, the third property is the numbers. So that is you know, understandable and totally awesome. Okay, the trouble is here, but when I convert the pupil into javascript, what I'm gonna get, I'm gonna get this simple array. If I just go there, you can see that, you know, this is an array, this is an array and this is an array. So in the in the javascript you just cannot find any difference between all of them because these are just simple arrays. But this is the benefit of the typescript now, you can see that you are using a thing of python in javascript, a beautiful and amazing thing of the python, The people in the javascript because of the typescript and now this is something which is not in the javascript. So this is the problem of the compiler, you do not need to worry about it. The pilot, the compiler will do the rest of the job and you do not even need to see the Js code, you do not need to verify anything in the Js code. You just simply can close it and whatever you're writing here, if everything is fine and you're not getting any any red line or any error, that means your code is going fine and you just don't need to need to panic or rush. So you do not need to even see the javascript. Uh you know, the more we work on it, the more we will be realizing that, you know, we are gonna have the classes, we're gonna have a lot of things in the uh in the T. S. And things would be different in the javascript and you will learn that. So everything will be handled by the compiler. This is not our problem. We're going to do a lot of things, a lot of things which are not the part of the um the, you know, as I mentioned, the javascript, but that's okay. So in the next video, we're absolutely going to talk about something more new in the in the typescript until then stay tuned.
99 Views 0 Likes 0 Comments

Arrays and Tuple

Comment
Leave a comment (supports markdown format)