This course has been updated! We now recommend you take the TypeScript Fundamentals, v3 course.
Transcript from the "Arrays & Tuples" Lesson
[00:00:00]
>> Mike North: Let's touch on arrays. Right, so we've been working with primitive values so far, and with questions we got a couple object examples. So here's an example of an array and pretty intuitive. One way of representing an array is to use square brackets following a type, right? So this is an array of numbers.
[00:00:21] We can push 33, and as you would expect, trying to push a string into this array will cause a problem. Now, if we didn't have this here, we would run into issues. That's an array of nevers, sounds bad. Array of nevers as in, it can never happen, it's never going to work.
[00:00:45] [LAUGH] So never is type script bottom type. And we will look at that more in detail later when we're studying top and bottom types specifically. But just note that we're gonna have problems pushing anything at all into this. And if we think about it this is kind of makes sense, if you could only look at line 57 and you had to decide the type of aa, all you can assume is that it's an array of whatever, it's an array of something.
[00:01:18] Now you could say it's an array of anything at all, but,
>> Mike North: That's gonna be dangerously flexible, right? So an array of nevers, yes it's an array. But without additional information. TypeScript doesn't have what it needs in order to make a nice, safe choice for you. So that's why for array declarations.
[00:01:41] That start out as empty. You wanna be specific about what you're putting in there. Now, if we had even one element, it's gonna be able to make a reasonable assumption here. So if you didn't have motivation in situations where you already start out with something that'll definitely be there and then conditionally you add other stuff.
[00:02:02] Putting that in the declaration itself as the variable initialization. Now you have motivation to go and do that, because it saves you from having to actually specify that type. It can be inferred if you give it some contents at all.
>> Mike North: So, tuple is a fancy word. Just means array.
[00:02:26] It's an array of a fixed length, and an array that comes with a convention. So if we were to define a data structure that could contain a street address, we could use an object with property names like house number, street name, postal code, city. And then we can pluck things out as we need them.
[00:02:47] Tuples, similarly, can hold a structured set of data. But, we just have to remember that the convention here is, the first thing is the house number, the second thing is the street name, the last thing is the postal code. And we still get strong typing there. So these arrays can only be of length four.
[00:03:06] And the types of their members must be exactly as we declare them. Now, this is a great way of returning multiple values from a function. I was successful in making this fetch and here is your data. I was unsuccessful in making this fetch, and here is your error message, right?
[00:03:26] A nice alternative, if you have something that, through regular use, might throw. Instead of throwing, which is good for exceptional errors, things you didn't plan for, this would allow you to return in like a thumbs up a thumbs down for the result and data about the details. You can see here that we get type checking for members of a tuple, one thing I want to point out that make it tight in the future
[00:04:00]
>> Mike North: You get type safety around the members, but I believe.
>> Mike North: You don't get type safety in push. So it's gonna regard this as in terms of the type of push, it can take strings or numbers. So with tuples, you're gonna want to be careful to just sort of set its value and access its value and not use the array methods because they're not really type safe.
[00:04:30] There's actually no way to type, to safely type push In such a way that based on the number of elements it currently has. If we were to try to incrementally build this up, we wouldn't know that first we should accept a number, then we should accept a string, then we should accept a string, then we should accept a number.
[00:04:52] With that it will require us to kind of based on state in the array change the type signature of this function. And you don't use tuples this way, anyway. Cuz the only way to get a valid one is to set it all at once and that can all be type checked as a whole statement.
[00:05:08] Yes?
>> Speaker 2: If you did, for example, want to have an array that could be either number or a string, how would you go about doing that?
>> Mike North: Good question. If I showed you
>> Mike North: If I showed you this tool tip here. Wait, sorry it's,
>> Mike North: This tool tip.
[00:05:35] So here, this kind of an example. If you had read that, like can someone give me a shot.
>> Speaker 3: Or a number there.
>> Mike North: String or number. It's string or number parentheses, entered an array of them.
>> Speaker 3: So you could do like a string in a function like I wanna array of strings or functions to return back in this the sum of that?
[00:05:54]
>> Mike North: Correct.
>> Speaker 3: Okay, cool.
>> Mike North: And that's the last thing we'll cover in this lecture.
>> Speaker 3: I'm sorry.
>> Mike North: No, no, do not apologize for leading us to if anything, you're doing me a favor and teeing up the next sections. Other questions about this?
>> Speaker 3: You can'actually just put any like
>> Mike North: Totally, yep, you could do any You can always provide a type indentation to loosen things up.
[00:06:21] Similar to our let and const example, when creating these tuples, right. These things of fixed length. Like a pair of numbers in an array. You are going to often have to or always have to specify the type you want. Why, because erring on the side of not interfering with your work flow.
[00:06:47] Type script is gonna consider this to be an array of numbers. Right, it's not gonna consider this to be a tuple containing two numbers. So you just have to always provide this type annotation here because it's a pretty specific thing. Same goes with a string literal type, if you wanted a string whose type was only the string Mike, it wouldn't be enough to just say let x equal Mike.
[00:07:11] You'd have to say let x: Mike = Mike. And only then would you get that really, really specific thing.