Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

require-at-least-one.d.ts 809B

1234567891011121314151617181920212223242526272829303132
  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<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 keys to each mapped type.
  27. & Except<ObjectType, KeysType>;