Dev Helpers
← Back

Map a page while preserving its cursor

Map a page while preserving its cursor using TypeScript.

Open this example in the interactive library

Source code

type Page<T> = { items: T[]; next: string | null };
const page: Page<{ id: number }> = { items: [{ id: 1 }, { id: 2 }], next: "2" };
console.log(
  (<A, B>(p: Page<A>, fn: (v: A) => B): Page<B> => ({
    ...p,
    items: p.items.map(fn),
  }))(page, (v) => v.id),
);

Sample output

{ items: [ 1, 2 ], next: '2' }