Dev Helpers
← Back

Take the first page of records

Take the first page of records 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,>(values: T[], size: number): Page<T> => ({
    items: values.slice(0, size),
    next: values.length > size ? String(size) : null,
  }))([1, 2, 3], 2),
);

Sample output

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