572ac0fb57519d283c7ae755d004d2cf5570739db27b7c8082b892864df83ffbfc5a26cb85e2026fe317a3fde6fb0acd854e5e50dcbad5a05f6c27476fde62 829 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /**
  2. Create a union of the given object's values, and optionally specify which keys to get the values from.
  3. Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/31438) if you want to have this type as a built-in in TypeScript.
  4. @example
  5. ```
  6. // data.json
  7. {
  8. 'foo': 1,
  9. 'bar': 2,
  10. 'biz': 3
  11. }
  12. // main.ts
  13. import {ValueOf} from 'type-fest';
  14. import data = require('./data.json');
  15. export function getData(name: string): ValueOf<typeof data> {
  16. return data[name];
  17. }
  18. export function onlyBar(name: string): ValueOf<typeof data, 'bar'> {
  19. return data[name];
  20. }
  21. // file.ts
  22. import {getData, onlyBar} from './main';
  23. getData('foo');
  24. //=> 1
  25. onlyBar('foo');
  26. //=> TypeError ...
  27. onlyBar('bar');
  28. //=> 2
  29. ```
  30. */
  31. export type ValueOf<ObjectType, ValueType extends keyof ObjectType = keyof ObjectType> = ObjectType[ValueType];