Dev Helpers
← Back

Reverse without mutating a readonly source

Reverse without mutating a readonly source using TypeScript.

Open this example in the interactive library

Source code

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

Sample output

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