Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

entry.d.ts 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. type MapKey<BaseType> = BaseType extends Map<infer KeyType, unknown> ? KeyType : never;
  2. type MapValue<BaseType> = BaseType extends Map<unknown, infer ValueType> ? ValueType : never;
  3. export type ArrayEntry<BaseType extends readonly unknown[]> = [number, BaseType[number]];
  4. export type MapEntry<BaseType> = [MapKey<BaseType>, MapValue<BaseType>];
  5. export type ObjectEntry<BaseType> = [keyof BaseType, BaseType[keyof BaseType]];
  6. export type SetEntry<BaseType> = BaseType extends Set<infer ItemType> ? [ItemType, ItemType] : never;
  7. /**
  8. Many collections have an `entries` method which returns an array of a given object's own enumerable string-keyed property [key, value] pairs. The `Entry` type will return the type of that collection's entry.
  9. For example the {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries|`Object`}, {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/entries|`Map`}, {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/entries|`Array`}, and {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/entries|`Set`} collections all have this method. Note that `WeakMap` and `WeakSet` do not have this method since their entries are not enumerable.
  10. @see `Entries` if you want to just access the type of the array of entries (which is the return of the `.entries()` method).
  11. @example
  12. ```
  13. import {Entry} from 'type-fest';
  14. interface Example {
  15. someKey: number;
  16. }
  17. const manipulatesEntry = (example: Entry<Example>) => [
  18. // Does some arbitrary processing on the key (with type information available)
  19. example[0].toUpperCase(),
  20. // Does some arbitrary processing on the value (with type information available)
  21. example[1].toFixed(),
  22. ];
  23. const example: Example = {someKey: 1};
  24. const entry = Object.entries(example)[0] as Entry<Example>;
  25. const output = manipulatesEntry(entry);
  26. // Objects
  27. const objectExample = {a: 1};
  28. const objectEntry: Entry<typeof objectExample> = ['a', 1];
  29. // Arrays
  30. const arrayExample = ['a', 1];
  31. const arrayEntryString: Entry<typeof arrayExample> = [0, 'a'];
  32. const arrayEntryNumber: Entry<typeof arrayExample> = [1, 1];
  33. // Maps
  34. const mapExample = new Map([['a', 1]]);
  35. const mapEntry: Entry<typeof map> = ['a', 1];
  36. // Sets
  37. const setExample = new Set(['a', 1]);
  38. const setEntryString: Entry<typeof setExample> = ['a', 'a'];
  39. const setEntryNumber: Entry<typeof setExample> = [1, 1];
  40. ```
  41. */
  42. export type Entry<BaseType> =
  43. BaseType extends Map<unknown, unknown> ? MapEntry<BaseType>
  44. : BaseType extends Set<unknown> ? SetEntry<BaseType>
  45. : BaseType extends unknown[] ? ArrayEntry<BaseType>
  46. : BaseType extends object ? ObjectEntry<BaseType>
  47. : never;