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.
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.
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.
type Args = [string, number?, ...boolean[]];
readonly arrays
readonly T[] and ReadonlyArray<T> are the same immutable view. A readonly tuple adds immutability per position.
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.
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.
Exercise 1Predict
What is the type of `pair`? ```ts const pair = ["age", 30] as const; ```
Exercise 2Implement
Implement `head<T>(values: readonly [T, ...T[]]): T` that returns the first element. The tuple type guarantees at least one element.
starter.typescriptexport function head<T>(values: readonly [T, ...T[]]): T { // your code }Reveal the reference answer
reference.typescriptexport 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.
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.typescriptfunction 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.typescriptfunction 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
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.