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

require-exactly-one.d.ts 1.3KB

123456789101112131415161718192021222324252627282930313233343536
  1. // TODO: Remove this when we target TypeScript >=3.5.
  2. // eslint-disable-next-line @typescript-eslint/generic-type-naming
  3. type _Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
  4. /**
  5. Create a type that requires exactly one of the given keys and disallows more. The remaining keys are kept as is.
  6. Use-cases:
  7. - Creating interfaces for components that only need one of the keys to display properly.
  8. - Declaring generic keys in a single place for a single use-case that gets narrowed down via `RequireExactlyOne`.
  9. The caveat with `RequireExactlyOne` is that TypeScript doesn't always know at compile time every key that will exist at runtime. Therefore `RequireExactlyOne` can't do anything to prevent extra keys it doesn't know about.
  10. @example
  11. ```
  12. import {RequireExactlyOne} from 'type-fest';
  13. type Responder = {
  14. text: () => string;
  15. json: () => string;
  16. secure: boolean;
  17. };
  18. const responder: RequireExactlyOne<Responder, 'text' | 'json'> = {
  19. // Adding a `text` key here would cause a compile error.
  20. json: () => '{"message": "ok"}',
  21. secure: true
  22. };
  23. ```
  24. */
  25. export type RequireExactlyOne<ObjectType, KeysType extends keyof ObjectType = keyof ObjectType> =
  26. {[Key in KeysType]: (
  27. Required<Pick<ObjectType, Key>> &
  28. Partial<Record<Exclude<KeysType, Key>, never>>
  29. )}[KeysType] & _Omit<ObjectType, KeysType>;