Basic

Arrays and Tuples

Type arrays of the same element type, and use tuples for fixed-length mixed collections.

What you will see

Ada 30

About 2 min of reading

Quick check

Before this new lesson, one small question about the lesson you just finished. Recall it, do not look it up.

Try It

Arrays and tuples carry their element types at compile time; the `[]` and tuple annotations disappear after compilation. Exercise `.push()`, indexing, and `.map()` on typed arrays.

1

Declaring typed arrays

const names: string[] = ["Ada", "Bo"]; const scores: Array = [90, 85]; console.log(names, scores);

Before you run it, say out loud what it will print. Then open the answer.

What it prints
[ 'Ada', 'Bo' ] [ 90, 85 ]
2

Reading and pushing

const colors: string[] = ["red"]; colors.push("blue"); console.log(colors[0], colors);

Before you run it, say out loud what it will print. Then open the answer.

What it prints
red [ 'red', 'blue' ]
3

Tuples for mixed fixed data

const point: [number, number] = [10, 20]; const user: [string, number] = ["Ada", 30]; console.log(user[0], user[1]);

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Ada 30

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

An array stores many values of one type; the compiler tracks that element type through every method.
1 Create array with element type -> records T
↓
2 Push items -> each checked against T
↓
3 map/filter/find -> return typed results
↓
4 Index access -> returns T (or T | undefined for find)
Why this works
Array methods stay typed

Because the element type is known, array methods return and accept the right types:

const nums: number[] = [1, 2, 3];
const doubled = nums.map((n) => n * 2);   // number[], no manual annotation
const found = nums.find((n) => n > 1);     // number | undefined

.find() returns T | undefined because a match is not guaranteed, the compiler forces you to handle the not-found case.

When you have a choice

When to use a tuple vs an interface

Tuples are positional and short; interfaces are named and self-documenting:

const tuple: [string, number] = ["Ada", 30];   // order matters, no labels
interface Person { name: string; age: number; } // named, clear intent

Use a tuple for small homogeneous-length data (coordinates, color RGB) and an interface/type for anything that benefits from named fields.

Examples

Declaring typed arrays

Arrays of one type

const names: string[] = ["Ada", "Bo"];
const scores: Array<number> = [90, 85];

Both string[] and Array<string> are valid. Array<T> is the generic form; T[] is the shorthand. Both mean "an array whose elements are all of type T".

Reading and pushing

Working with arrays

const colors: string[] = ["red"];
colors.push("blue");     // ok: string
colors.push(123);        // error: number not assignable to string
console.log(colors[0]);  // "red"

.push() is also type-checked: you cannot add the wrong element type.

Tuples for mixed fixed data

Tuples

const point: [number, number] = [10, 20];
const user: [string, number] = ["Ada", 30];
console.log(user[0], user[1]);  // Ada 30

A tuple has a fixed length and order with a specific type per position. user[0] is a string, user[1] is a number.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Primitive Types
Previous Lesson
Primitive Types
Next Lesson
Typed Functions
Typed Functions →