TypeScript Snippet

RequireAtLeastOne Utility Type

Difficulty: Medium

API endpoints often want 'pass at least one of these search params, but not necessarily all of them' (`{ id }`, `{ email }`, `{ id, email }`, but never `{}`). TypeScript's stock utilities cannot express that constraint, but a small `RequireAtLeastOne<T>` does. This snippet builds the type, layers in a `RequireExactlyOne` variant for either-or constraints, and shows a runtime guard that pairs with both.

Code Snippets
/

RequireAtLeastOne Utility Type

RequireAtLeastOne Utility Type

API endpoints often want 'pass at least one of these search params, but not necessarily all of them' (`{ id }`, `{ email }`, `{ id, email }`, but never `{}`). TypeScript's stock utilities cannot express that constraint, but a small `RequireAtLeastOne<T>` does. This snippet builds the type, layers in a `RequireExactlyOne` variant for either-or constraints, and shows a runtime guard that pairs with both.

TypeScript
Medium
3 snippets
ts-utility-types
ts-mapped-types
ts-conditional-types
ts-generics

455 views

2

The type expands into a union: for each candidate key K, produce a shape where K is required and the other keys are optional, then union all of those shapes together. Pick<T, Exclude<keyof T, Keys>> keeps any non-candidate keys intact (useful when only a subset of T's keys participate in the constraint). The runtime sentinel only fills one key at a time, exactly the contract you want at the API boundary. Without this constraint, Partial<SearchInput> would silently allow {}, which usually means a buggy caller hitting the database with no filter.