Check out a free preview of the full TypeScript Fundamentals course:
The "Arrays & Tuples" Lesson is part of the full, TypeScript Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Mike reviews arrays in TypeScript, which work the same way as in JavaScript. Mike takes questions from students.

Get Unlimited Access Now

Transcript from the "Arrays & Tuples" Lesson

[00:00:00]
>> Mike North: So arrays. I hinted at this going through the solution to the last exercise. You could have left that users property in the account manager as an array of anys and I think it would have been okay. It would just asked you to assign it a particular type.

[00:00:19] But the default is they work exactly the same way as JavaScript and this is, basically gets an implicit any in this situation. Adding the type constraint makes things a lot more consistent, a lot better, that's what this would look like, right? Hopefully that's no surprise, it's just the type and then square brackets.

[00:00:38] Just remember, when you initialize those class properties with empty arrays, make sure you provide a type. You will find that in this situation, you will run into trouble. Because the type of items will be an array of nevers. An array of a type that would never be happy if you try to do something with it.

[00:01:00] So the consequences like that, you will error when you try to push the number five into it. So you would simply have to say like, an array of numbers or an array of any, whatever it is that you want, but be sure you give it a type.
>> Speaker 2: A little default to nevers and not to give you a warning or.

[00:01:19]
>> Mike North: It'll default to nevers, and be uncooperative if you ever start trying to put stuff into it.
>> Speaker 2: Yeah, okay.
>> Mike North: It will let you know that there's a problem, for sure. And that's the kind of problem it'll give you. It can't map five to a never. And here's the solution, just do something like that.

[00:01:37] This is a good practice anyway. And the reasoning here is, at the time we define a class, remember we only have, we have like, compile time type safety here. We don't have runtime type safety. And until you start creating instances of this class, there's no way to really know exactly what you aim to push into this.

[00:01:59] And so at least at that moment where you just have that array property, it's tough to know like the orders of functions that you're gonna call on this thing. So that's why you end up with that array of nevers. Question in chat?
>> Speaker 3: Yeah, he's just asking if there's any way to set the maximum number of array elements in TypeScript.

[00:02:18]
>> Mike North: Maximum, like on any array? The maximum number of-
>> Speaker 3: No, just like in an array, for instance-
>> Mike North: Yes.
>> Speaker 3: Number array.
>> Mike North: So there is a way to define a specific number of array elements. And that is by way of something called tuples.
>> Speaker 3: But there is no way they do it like a max or a minimum amount?

[00:02:40]
>> Mike North: Not that I am aware of, I don't think so. So tuples are an array of fixed length. So if you had a specific size of array that you were interested in, you could do something like this. You could say, and here we're able to mix types, we're basically saying that this is an ordered collection.

[00:03:00] We would say that this is a tuple of size two, right? There are two elements in it. The first one must be a string, the second one must be a number and so we can do something like this. And we would be absolutely fine. So often like when we combine these things into a value, it's because it's two pieces of information that means something together, right.

[00:03:24] And any consumers of this information need to know what this means, they need to expect the right pattern coming out. TypeScript's not gonna help you except in terms of defining the types. By that I mean, of course you don't have property names here, you've got just the zeroth element on the first element.

[00:03:43] And so you as a consumer need to know you are going to receive a tuple, and here is where in the tuple you can look for particular pieces of data and what that data means. So destructured assignment works really nicely with this where you can sort of, you could say pluck off the name of the dependency and its version and you can basically unpack the tuple really, really, easily.

[00:04:05] Just cuz you know, like if you have the information to use this, you have all the information you need to destructure this and unpack it.
>> Mike North: So if we were to create an array of tuples like we can regardless, is just another type. Just as we could create an array of numbers.

[00:04:25] A tuple type is equally well suited for that kind of thing. We could push that dependency in and everything would be fine. We could push in something literally that, like an array literal, a tuple literal I guess. That would cooperate, and here we'd run into an error. So the type checking works on members, of members of the tuple within the array so you start to see that like that structure that checking is done recursively.

[00:04:57] Right, it's not just like, this is an array of arrays and make sure that only arrays are pushed in. It looks for that specific structure, the elements in the correct order. In fact if we tried to reverse these and push the number first and the string second, of course it's gonna break as well.

[00:05:13] But this is nice cuz now you can be assured that everything in this array is going to be of that same format.