- Регистрация
- 1 Мар 2015
- Сообщения
- 1,481
- Баллы
- 155
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.
In 2025, TypeScript makes it cleaner than ever with a pattern called RequireOnlyOne. One small generic gives you:
? 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.