Skip to content

Arrays and Tuples in TypeScript

Type arrays with element types and tuples with fixed-length, position-typed arrays.

What you'll learn

  • Two syntaxes for array types: T[] and Array<T>
  • How tuples fix the type at each position
  • Readonly arrays and tuples for immutability
  • Common array operations and their type signatures

Concept

Array Types

An array of a specific element type is written as T[] or Array<T> — both are equivalent. For example, number[] is an array of numbers and string[] is an array of strings. You can nest them: number[][] is a 2D array (array of arrays of numbers). TypeScript also infers array element types from array literals, widening to the common type: [1, 2, 3] infers as number[], while [1, "two"] infers as (string | number)[].

Tuple Types

A tuple is an array with a fixed number of elements, each with a specific type. You write it as [Type1, Type2, ...]. For example, [string, number] is a pair where position 0 is a string and position 1 is a number. Tuples are useful for returning multiple values, representing CSV rows, or modeling coordinate pairs. You can mark them readonly with readonly [string, number] to prevent mutation.

Readonly Arrays

The readonly modifier prevents array mutation: readonly number[] lets you read elements but not push, pop, or assign by index. This is useful for function parameters that should not be mutated. The ReadonlyArray<T> type is the equivalent named form. Readonly arrays enforce immutability at the type level, catching accidental mutations at compile time.

Example

              // Array types — two equivalent syntaxes.
let scores: number[] = [90, 85, 92];
let names: Array<string> = ["Alice", "Bob", "Carol"];

// 2D array — array of arrays.
let grid: number[][] = [[1, 2], [3, 4]];

// Tuple — fixed length, each position has a specific type.
let httpOk: [number, string] = [200, "OK"];
let point: [number, number] = [10, 20];

// Readonly tuple — cannot be mutated.
const fixed: readonly [string, number] = ["id", 42];
// fixed[0] = "name"; // ❌ Error: readonly

// Tuple with optional trailing element.
type Result = [number, string?];
const success: Result = [200, "done"];
const bare: Result = [404];

console.log(scores[0], httpOk[1], point, success, bare);
            

Arrays use T[] or Array<T>. Tuples fix length and per-position types. readonly prevents mutation. Optional tuple elements use ? at the end.

Try it

Common mistakes

The mistake

Treating a tuple like a regular array and pushing extra elements

The fix

Tuples have a fixed length. Pushing beyond the defined length violates the type. If the length varies, use a regular array type T[] instead.

The mistake

Forgetting that tuple element order matters

The fix

[string, number] and [number, string] are different types. Position 0 must match the first declared type. Always check the order when assigning.

Practice Arrays and Tuples in TypeScript

9 exercises

Practice Arrays and Tuples in TypeScript

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors