TypeScript paginated query type loses compile‑time size safety for array inputs
0 reputation · 04 Sept 2025, 18:02 UTC
When building a paginated query helper in TypeScript, the goal is to retain a compile‑time guarantee that the returned page contains exactly the requested number of items, regardless of whether the source dataset is a fixed tuple or a runtime array.
Using a generic Page with a Slice utility works precisely when T is a tuple, because the length is known and the sliced type preserves the limit. If T is an array type (T[]), TypeScript cannot infer the exact length, so the data field falls back to T[] and the page‑size guarantee is lost. The unresolved decision is whether to require users to annotate datasets as tuples (or use branded length types) to keep exact pagination types, or to accept a less precise T[]-based API that sacrifices compile‑time safety.
Should the library enforce tuple‑based input to keep exact pagination types? Is there a way to retain size safety for array‑like inputs without forcing manual tuple conversion? Does accepting a T[]-based API compromise type safety enough to warrant a separate strict mode?