Skip to article content
FrontendXLesson 13 / 40

Tuples, readonly arrays and const assertions

Go from the first annotated variable to writing your own conditional, mapped and infer-based utility types, while keeping a clear line between compile-time checking and runtime behavior.

intermediate · about 45 minutes · 3 exercises

  • Predict
  • Implement
  • Debug

What you will be able to do

#
  • Read a tuple type and describe its fixed length and per-position types.
  • Distinguish readonly T[] from ReadonlyArray<T> and a readonly tuple.
  • Use as const to keep a literal tuple instead of widening to an array.

Overview

#

Tuples are fixed-shape arrays

A tuple lists the type of each position.

snippet.ts
type Pair = [string, number];
const p: Pair = ["age", 30];

p[0] is string and p[1] is number. Assigning ["age"] or ["age", 30, 1] fails because the length is part of the type.

Optional and rest elements

A trailing element can be optional with ?, and a rest element collects the tail.

snippet.ts
type Args = [string, number?, ...boolean[]];

readonly arrays

readonly T[] and ReadonlyArray<T> are the same immutable view. A readonly tuple adds immutability per position.

snippet.ts
function sum(values: readonly number[]): number {
  return values.reduce((total, n) => total + n, 0);
}

The parameter accepts both mutable and readonly arrays, and the body cannot push.

as const

as const turns an array literal into a readonly tuple of literal types.

snippet.ts
const pair = ["age", 30] as const; // readonly ["age", 30]

Without it, the same literal widens to (string | number)[].

Why it matters

Tuples model fixed argument lists, coordinate pairs and Object.entries results. readonly arrays express that you read a collection without mutating it, which callers can rely on.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Predict

    What is the type of `pair`? ```ts const pair = ["age", 30] as const; ```

    Choose the best answer
  2. Exercise 2Implement

    Implement `head<T>(values: readonly [T, ...T[]]): T` that returns the first element. The tuple type guarantees at least one element.

    starter.typescript
    export function head<T>(values: readonly [T, ...T[]]): T {
      // your code
    }
    

    Reveal the reference answer
    reference.typescript
    export function head<T>(values: readonly [T, ...T[]]): T {
      return values[0];
    }
    

    Authored expected output: Compiles with no errors. Runtime: head([1, 2, 3]) is 1. The call head([]) fails to compile.

    The rest element in the tuple type guarantees index 0 exists, so no undefined check is needed.

  3. Exercise 3Debug

    This fails to compile. Fix it so the function accepts a readonly array without copying it. ```ts function total(values: number[]): number { return values.reduce((sum, n) => sum + n, 0); } const frozen = [1, 2, 3] as const; total(frozen); ```

    starter.typescript
    function total(values: number[]): number {
      return values.reduce((sum, n) => sum + n, 0);
    }
    const frozen = [1, 2, 3] as const;
    total(frozen);
    

    Reveal the reference answer
    reference.typescript
    function total(values: readonly number[]): number {
      return values.reduce((sum, n) => sum + n, 0);
    }
    const frozen = [1, 2, 3] as const;
    total(frozen);
    

    Authored expected output: Compile error resolved: 'readonly [1, 2, 3]' is not assignable to 'number[]'. With a readonly number[] parameter the call compiles.

    A readonly array is not assignable to a mutable array, but a mutable array is assignable to a readonly one, so widening the parameter is the correct fix.

Common misconceptions

#
  • A tuple and an array with the same elements are the same type.
  • readonly is enforced at runtime.
  • as const changes the runtime value.

Follow-up practice

#
  • typescript-enums-const-objects

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.