Dev Helpers

TypeScript / Immutable updates

Reverse without mutating a readonly source

Dev Helpers · August 24, 2025

Readonly inputs make accidental mutation a type error. A nonmutating reverse creates a new order while preserving the caller's original sequence.

Worked example

type Item = Readonly<{ id: number; label: string }>;
const items: readonly Item[] = [
  { id: 1, label: "one" },
  { id: 2, label: "two" },
];
console.log(items.toReversed());

Result

[ { id: 2, label: 'two' }, { id: 1, label: 'one' } ]

Implementation notes

Readonly is shallow unless the nested values are readonly as well. A new array order does not copy the records it contains.

Try it yourself

Keep a reference to the original array and compare it after the call.

Open the TypeScript example →

Editorial date August 24, 2025 · Published December 6, 2026

Related articles

Reject fractional cent amounts

A numeric type cannot by itself guarantee whole cents. Validate integrality where money enters the typed part of the application.

Infer route names from a registry

A route registry can drive both lookup and allowed route names. Type the keys from the registry so a removed route cannot survive in a separate union.