Skip to content

Arrays, tuples and readonly

Relay is started like this:

Terminal window
relay --port 8080 --queue localhost:6379

process.argv.slice(2) gives ["--port", "8080", "--queue", "localhost:6379"]. Two shapes are hiding in there: a list of [flag, value] pairs, and a host:port pair inside one value. Arrays model the first, tuples the second, and readonly keeps the parser from damaging its input.

const ports: number[] = [8080, 8081]
const hosts: Array<string> = ["a", "b"] // same type, generic spelling
const mixed: (string | number)[] = ["a", 1]

With noUncheckedIndexedAccess, reading by index includes undefined, because the index may be past the end. Destructuring an array works the same way. A for...of loop doesn’t, because it only visits elements that exist:

const argv: string[] = ["--port", "8080"]
const first = argv[0] // string | undefined
const [flag, value] = argv // both string | undefined
for (const arg of argv) {
arg.toUpperCase() // arg is string
}
// @ts-expect-error: first may be undefined
first.toUpperCase()

readonly string[] (or ReadonlyArray<string>) is an array without the methods that change it: no push, pop, shift, splice, sort or reverse, and no assignment by index.

function parse(argv: readonly string[]) {
// @ts-expect-error: property 'shift' does not exist on readonly string[]
argv.shift()
const sorted = argv.toSorted() // fine: returns a new array
return sorted
}
parse(["--port", "8080"]) // a mutable array is accepted

A mutable array can be passed where a readonly one is expected, but not the other way around. So a function that only reads should declare its parameter readonly. It accepts more callers and promises not to change what they pass in.

A tuple is an array with a fixed length and a type per position. Labels document each position and show up in hovers and errors:

type HostPort = [host: string, port: number]
const queue: HostPort = ["localhost", 6379]
const [host, port] = queue // string, number: no undefined, the length is known
// @ts-expect-error: the second element must be a number
const bad: HostPort = ["localhost", "6379"]
// optional and rest elements
type Endpoint = [host: string, port?: number]
type Command = [name: string, ...args: string[]]
const e: Endpoint = ["localhost"]
const c: Command = ["relay", "--port", "8080"]

Make tuples readonly as well, or a caller can write queue[1] = 0 or queue.push(...) on what was meant to be a fixed pair:

type HostPort = readonly [host: string, port: number]
const queue: HostPort = ["localhost", 6379]
// @ts-expect-error: cannot assign to '1' because it is a read-only property
queue[1] = 0

Make both parsers check the input, return readonly results, and leave argv as it was. The hidden tests check the return types too.

  • With noUncheckedIndexedAccess, indexing and destructuring include undefined. for...of doesn’t.
  • Parameters you only read should be readonly. Mutable arrays are still accepted.
  • Use toSorted and friends instead of the mutating methods.
  • Tuples have a fixed length and a type per position. Label them, and make them readonly.