Ever built a function that should accept one of several options β but not more, not less?
In 2025, TypeScript makes it cleaner than ever with a pattern called RequireOnlyOne. One small generic gives you:
β
Precise API contracts
β No runtime ambiguity
π₯ Compile-time enforcement
π Use cases:
β’ A weather API: cityId or coordinates
β’ A React Avatar: initials or photo URL
β’ An invoice handler: invoiceId or subscriptionId
π§ Bonus insight: works great with OpenAPI, and easy to adapt into RequireAtLeastOne.
Skip the boilerplate, prevent misuse, and make future refactors effortless.
π Like real-world TS tips? Follow for more clean patterns, no fluff.
https://levelup.gitconnected.com/requireonlyone-in-2025-typescript-trick-for-exactly-one-property-8637ca5e6907
Top comments (0)