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)