28.3. Arrays in TypeScript

Arrays in TypeScript must contain values of the same type. When declaring an array, the type needs to be declared.

1
let 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.

1
2
3
let tupleName: [number, string, number];

tupleName = [10, "9", 8];

28.3.1. Examples

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:

1
2
3
let element1 = ['hydrogen', 'H', 1.008];
let element2 = ['helium', 'He', 4.003];
let element26 = ['iron', 'Fe', 55.85];

In TypeScript, however, an array can only hold values of one type, so we need to use a tuple.

1
2
3
4
5
6
7
8
let element1: [string, string, number];
element1 = ['hydrogen', 'H', 1.008];

let element2: [string, string, number];
element2 = ['helium', 'He', 4.003];

let element26: [string, string, number];
element26 = ['iron', 'Fe', 55.85];

28.3.2. Check Your Understanding

Question

Which of the following statements is FALSE about a tuple in TypeScript?

  1. Tuples can hold as many elements as needed.

  2. Tuples hold values of one type.

  3. When declaring a tuple, programmers include the types of the values in a tuple.