[6a3a178] | 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 |
|
---|
| 4 | export type ArrayEntry<BaseType extends readonly unknown[]> = [number, BaseType[number]];
|
---|
| 5 | export type MapEntry<BaseType> = [MapKey<BaseType>, MapValue<BaseType>];
|
---|
| 6 | export type ObjectEntry<BaseType> = [keyof BaseType, BaseType[keyof BaseType]];
|
---|
| 7 | export type SetEntry<BaseType> = BaseType extends Set<infer ItemType> ? [ItemType, ItemType] : never;
|
---|
| 8 |
|
---|
| 9 | /**
|
---|
| 10 | 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.
|
---|
| 11 |
|
---|
| 12 | 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.
|
---|
| 13 |
|
---|
| 14 | @see `Entries` if you want to just access the type of the array of entries (which is the return of the `.entries()` method).
|
---|
| 15 |
|
---|
| 16 | @example
|
---|
| 17 | ```
|
---|
| 18 | import {Entry} from 'type-fest';
|
---|
| 19 |
|
---|
| 20 | interface Example {
|
---|
| 21 | someKey: number;
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | const manipulatesEntry = (example: Entry<Example>) => [
|
---|
| 25 | // Does some arbitrary processing on the key (with type information available)
|
---|
| 26 | example[0].toUpperCase(),
|
---|
| 27 |
|
---|
| 28 | // Does some arbitrary processing on the value (with type information available)
|
---|
| 29 | example[1].toFixed(),
|
---|
| 30 | ];
|
---|
| 31 |
|
---|
| 32 | const example: Example = {someKey: 1};
|
---|
| 33 | const entry = Object.entries(example)[0] as Entry<Example>;
|
---|
| 34 | const output = manipulatesEntry(entry);
|
---|
| 35 |
|
---|
| 36 | // Objects
|
---|
| 37 | const objectExample = {a: 1};
|
---|
| 38 | const objectEntry: Entry<typeof objectExample> = ['a', 1];
|
---|
| 39 |
|
---|
| 40 | // Arrays
|
---|
| 41 | const arrayExample = ['a', 1];
|
---|
| 42 | const arrayEntryString: Entry<typeof arrayExample> = [0, 'a'];
|
---|
| 43 | const arrayEntryNumber: Entry<typeof arrayExample> = [1, 1];
|
---|
| 44 |
|
---|
| 45 | // Maps
|
---|
| 46 | const mapExample = new Map([['a', 1]]);
|
---|
| 47 | const mapEntry: Entry<typeof mapExample> = ['a', 1];
|
---|
| 48 |
|
---|
| 49 | // Sets
|
---|
| 50 | const setExample = new Set(['a', 1]);
|
---|
| 51 | const setEntryString: Entry<typeof setExample> = ['a', 'a'];
|
---|
| 52 | const setEntryNumber: Entry<typeof setExample> = [1, 1];
|
---|
| 53 | ```
|
---|
| 54 | */
|
---|
| 55 | export type Entry<BaseType> =
|
---|
| 56 | BaseType extends Map<unknown, unknown> ? MapEntry<BaseType>
|
---|
| 57 | : BaseType extends Set<unknown> ? SetEntry<BaseType>
|
---|
| 58 | : BaseType extends unknown[] ? ArrayEntry<BaseType>
|
---|
| 59 | : BaseType extends object ? ObjectEntry<BaseType>
|
---|
| 60 | : never;
|
---|