Dev Helpers
← Back

Read a cursor as a nonnegative offset

Read a cursor as a nonnegative offset 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(
  ((cursor: string | null) =>
    cursor && /^\d+$/.test(cursor) ? Number(cursor) : 0)(page.next),
);

Sample output

2