# Arrays & Tuples

> Dynamic lists vs fixed-length pairs.

Subject: TypeScript | Lesson 5 of 54 | 3 min read
Interactive lesson: https://codewithanimation.in/typescript/arrays-and-tuples
Previous: [The void Type](https://codewithanimation.in/typescript/void-type.md) | Next: [Objects & Interfaces](https://codewithanimation.in/typescript/object-types-interfaces.md)

## Two ways to type a list

In JavaScript, an array is just a dynamic list that can hold anything. In TypeScript, we split this concept into two distinct types: Arrays and Tuples.

An **Array** (`string[]` or `Array<string>`) holds any number of elements, but they must all share the same type.

A **Tuple** (`[string, number]`) is an array with a fixed length and a known type at every specific index.

## Why use Tuples?

Tuples are perfect for situations where the position of the data has specific meaning. The most famous example is React's `useState` hook, which always returns exactly two elements: a state value at index 0, and a setter function at index 1.

If `useState` returned a standard array like `(number | Function)[]`, TypeScript wouldn't know which index held the number and which held the function.

## Defining them

You define an array by appending `[]` to a type:

```typescript
const names: string[] = ["Alice", "Bob"];
names.push("Charlie"); // OK
```

You define a tuple using square brackets around a comma-separated list of types:

```typescript
const userRecord: [number, string] = [1, "Alice"];
// Error: Source has 3 element(s) but target allows only 2.
const badRecord: [number, string] = [1, "Alice", "Admin"];
```

## Common mistake: The tuple mutation loophole

A famous quirk of TypeScript tuples is that while you cannot *initialize* them with the wrong number of elements, TypeScript (historically) allowed you to call `.push()` on them, mutating their length at runtime without a compiler error.

To truly lock down a tuple, you should make it `readonly`: `const user: readonly [number, string] = [1, "Alice"];`. Now, calling `.push()` will trigger a compile error.

## Tip: Array generic syntax

You will often see arrays typed as `Array<string>` instead of `string[]`. They mean the exact same thing. Most teams prefer `string[]` for simple primitives, and `Array<MyComplexType>` when the type name gets very long or complex.

## Try it: Try it

Try to push a third element into both a standard Array and a strictly-typed Tuple.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/typescript/arrays-and-tuples

## Check yourself

### Question 1

What is the primary difference between a standard Array and a Tuple in TypeScript?

- A. Arrays can hold multiple types, Tuples can only hold one type.
- B. Tuples have a fixed length and known types at specific indices.
- C. Tuples are immutable by default.

Correct answer: B

A Tuple explicitly defines how many elements exist and what type belongs at each specific index.

- Why not A: It's the exact opposite.
- Why not C: Tuples are mutable unless you explicitly prefix them with 'readonly'.

## Remember this

- Arrays (`string[]`) are for dynamic-length lists of the same type.
- Tuples (`[number, string]`) are for fixed-length lists where the position matters.
- React's `useState` is the most common real-world example of a Tuple.
- Use `readonly` to prevent accidental `.push()` mutations on Tuples.
