Arrays, tuples and readonly
Relay is started like this:
relay --port 8080 --queue localhost:6379process.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.
Arrays
Section titled “Arrays”const ports: number[] = [8080, 8081]const hosts: Array<string> = ["a", "b"] // same type, generic spellingconst 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 | undefinedconst [flag, value] = argv // both string | undefined
for (const arg of argv) { arg.toUpperCase() // arg is string}
// @ts-expect-error: first may be undefinedfirst.toUpperCase()readonly arrays
Section titled “readonly arrays”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 acceptedA 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.
Tuples
Section titled “Tuples”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 numberconst bad: HostPort = ["localhost", "6379"]
// optional and rest elementstype 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 propertyqueue[1] = 0Exercise
Section titled “Exercise”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 includeundefined.for...ofdoesn’t. - Parameters you only read should be
readonly. Mutable arrays are still accepted. - Use
toSortedand friends instead of the mutating methods. - Tuples have a fixed length and a type per position. Label them, and make them
readonly.
Further reading
Section titled “Further reading”- Handbook: Everyday Types: arrays and tuple types.
- Array.prototype.toSorted() (MDN): the copying sort used with readonly arrays.