DEV Community

Aleksei Aleinikov
Aleksei Aleinikov

Posted on

🎯 Exactly One. Never Zero. Never Two.

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)