Fixing Unsafe Tuples
Here we have a dangerousFunction
which accepts an array of numbers as an argument:
const dangerousFunction = (arrayOfNumbers: number[]) => { arrayOfNumbers.pop(); arrayOfNumbers.pop();};
Additionally, we've defined a variable myHouse
which is a tuple representing a `Coordinate
Transcript
00:00 In this exercise, we have a dangerous function here that takes in an array of numbers, and we have up at the top here, we have a coordinate, right? And the coordinate is a tuple. It's a tuple of two members here, so it has number and number there. And then we have const my
00:16 house is a coordinate, and it's basically just this tuple here. But there's something really kind of horrible happening here, which is we have our dangerous function, which we basically pass in my house to it. We pass in this coordinate, and inside the dangerous
00:32 function, it's actually going to pop two of the members here. And pop removes the last element from an array. So this means that just by calling dangerous function on my house here, it means that when we go to access my house, so my house two like this, or let's
00:48 say my house one const example equals my house one. This means that example is actually not going to be number, it's going to be undefined. Because after calling dangerous function on my house, my house is going to have length of zero. This is really annoying, a sort of
01:04 annoying little property of TypeScript. But there is something we can do to make this error go away. Your job is to try to find a better type for coordinates up here to basically make it so that we can't pass it into this dangerous function. The dangerous function
01:20 has got to stay exactly the same. We're basically just looking for this TS expect error to trigger for there to be an error when we try to pass my house into this dangerous function. Good