Essential Types And Notations 20 exercises
Problem

Using Optional Tuple Members in TypeScript

This goToLocation function takes in an array of coordinates. Each coordinate has a latitude and longitude, which are both numbers, as well as an optional elevation which is also a number:


const goToLocation = (coordinates: Array<number>) => {
const latitude = coordinates[0];

Loading exercise

Transcript

00:00 In this exercise, we have a function called goToLocation, which takes in an array of coordinates. These coordinates have latitude, longitude, and elevation on them. And we're expecting latitude to be type of number, longitude to be type of number, but elevation to be type of number or undefined.

00:17 And we're expecting that you can go to location in various different ways. So you should be able to go to location without passing an elevation, and you should be able to go to location with passing elevation. And of course, you shouldn't be able to pass a string in this number slot. So your job is to work out how we can use a tuple here,

00:35 but give it an optional member because elevation doesn't need to be included. It's a really interesting one. There's a couple of interesting pieces of syntax here, usually using the question mark character. So good luck.