4c996e63e6b085f5624448c91814bdd999f8a9868790b591790e6663407837e23cbe44e7731797ad4546308aea389e36259faf510f295546d55dac0713354a 827 B

1234567891011121314151617181920212223242526272829303132
  1. import {Except} from './except';
  2. /**
  3. Create a type that requires at least one of the given properties. The remaining properties are kept as is.
  4. @example
  5. ```
  6. import {RequireAtLeastOne} from 'type-fest';
  7. type Responder = {
  8. text?: () => string;
  9. json?: () => string;
  10. secure?: boolean;
  11. };
  12. const responder: RequireAtLeastOne<Responder, 'text' | 'json'> = {
  13. json: () => '{"message": "ok"}',
  14. secure: true
  15. };
  16. ```
  17. */
  18. export type RequireAtLeastOne<ObjectType, KeysType extends keyof ObjectType = keyof ObjectType> =
  19. {
  20. // For each Key in KeysType make a mapped type
  21. [Key in KeysType]: (
  22. // …by picking that Key's type and making it required
  23. Required<Pick<ObjectType, Key>>
  24. )
  25. }[KeysType]
  26. // …then, make intersection types by adding the remaining properties to each mapped type.
  27. & Except<ObjectType, KeysType>;