Dev Helpers
← Back

Collect items across pages

Collect items across pages 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(
  (<T,>(pages: Page<T>[]) => pages.flatMap((p) => p.items))([
    page,
    { items: [{ id: 3 }], next: null },
  ]),
);

Sample output

[ { id: 1 }, { id: 2 }, { id: 3 } ]