df5bec874a58244ff4d57bc2df4c55e782e17cf69dfdf416218d56d1d146c9745f8c9673d2b55fa0d5d3b371a2c966eea1d596f3f85717a5259a5de5179ef6 848 B

123456789101112131415161718192021222324252627282930313233
  1. import {Except} from './except';
  2. /**
  3. Create a type that requires at least one of the given keys. The remaining keys 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<
  19. ObjectType,
  20. KeysType extends keyof ObjectType = keyof ObjectType
  21. > = {
  22. // For each `Key` in `KeysType` make a mapped type:
  23. [Key in KeysType]-?: Required<Pick<ObjectType, Key>> & // 1. Make `Key`'s type required
  24. // 2. Make all other keys in `KeysType` optional
  25. Partial<Pick<ObjectType, Exclude<KeysType, Key>>>;
  26. }[KeysType] &
  27. // 3. Add the remaining keys not in `KeysType`
  28. Except<ObjectType, KeysType>;