Arrays in TypeScript must contain values of the same type. When declaring an array, the type needs to be declared.
1let arrayName: number[] = [10,9,8];
What if the array needs to hold values of different types?
Now, we need a tuple. A tuple is a special structure in TypeScript that can hold as many values as needed of different types.
1let tupleName: [number, string, number];
2
3tupleName = [10, "9", 8];
In JavaScript, we would declare an array holding items in our cargo hold like so:
let cargoHold = ['oxygen tanks', 'space suits', 'parrot', 'instruction manual', 'meal packs', 'slinky', 'security blanket'];
In TypeScript, we would declare the same cargoHold
array a little differently:
let cargoHold: string[] = ['oxygen tanks', 'space suits', 'parrot', 'instruction manual', 'meal packs', 'slinky', 'security blanket'];
What about declaring arrays for elements on the Periodic Table? In JavaScript, that is a relatively simple task:
1let element1 = ['hydrogen', 'H', 1.008];
2let element2 = ['helium', 'He', 4.003];
3let element26 = ['iron', 'Fe', 55.85];
In TypeScript, however, an array can only hold values of one type, so we need to use a tuple.
1let element1: [string, string, number];
2element1 = ['hydrogen', 'H', 1.008];
3
4let element2: [string, string, number];
5element2 = ['helium', 'He', 4.003];
6
7let element26: [string, string, number];
8element26 = ['iron', 'Fe', 55.85];
Question
Which of the following statements is FALSE about a tuple in TypeScript?