Skip to content

Commit

Permalink
Update entries.d.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus authored and dimitropoulos committed Sep 29, 2020
1 parent e89893c commit 670b561
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions source/entries.d.ts
Expand Up @@ -6,52 +6,52 @@ type ObjectEntries<BaseType> = Array<ObjectEntry<BaseType>>;
type SetEntries<BaseType extends Set<unknown>> = Array<SetEntry<BaseType>>;

/**
Many collections have a `.entries()` method that will return an enumerable array of that structure's keys and values. The `Entries` type will return the type of that collection's entries given the type itself.
Many collections have an `.entries()` method that will return an iterable of that structure's keys and values. The `Entries` type will return the type of that collection's entries given the type itself.
For example the `Object`, `Map`, `Array`, and `Set` collections all have this method. Note that since `WeakMap` and `WeakSet` do not have this method since their entries are not enumerable.
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.
@see `Entry` if you want to just access the type of a single entry.
@example
```
import {Entries} from 'type-fest';
interface Example {
someKey: number;
}
const manipulatesEntries = (examples: Entries<Example>) => {
return examples.map(example => [
// does some arbitrary processing on the key (with type information available)
example[0].toUpperCase(),
const manipulatesEntries = (examples: Entries<Example>) => examples.map(example => [
// Does some arbitrary processing on the key (with type information available)
example[0].toUpperCase(),
// does some arbitrary processing on the value (with type information available)
example[1].toFixed()
]);
};
// Does some arbitrary processing on the value (with type information available)
example[1].toFixed()
]);
const example: Example = { someKey: 1 };
const example: Example = {someKey: 1};
const entries = Object.entries(example) as Entries<Example>;
const output = manipulatesEntries(entries);
// Objects
const objectExample = { a: 1 };
const objectExample = {a: 1};
const objectEntries: Entries<typeof objectExample> = [['a', 1]];
// Maps
const mapExample = new Map([['a', 1]]);
const mapEntries: Entries<typeof map> = [['a', 1]];
// Arrays
const arrayExample = ['a', 1];
const arrayEntries: Entries<typeof arrayExample> = [[0, 'a'], [1, 1]];
// Maps
const mapExample = new Map([['a', 1]]);
const mapEntries: Entries<typeof map> = [['a', 1]];
// Sets
const setExample = new Set(['a', 1]);
const setEntries: Entries<typeof setExample> = [['a', 'a'], [1, 1]];
```
*/
export type Entries<BaseType> =
BaseType extends Map<unknown, unknown> ? MapEntries<BaseType>
: BaseType extends Set<unknown> ? SetEntries<BaseType>
: BaseType extends unknown[] ? ArrayEntries<BaseType>
: BaseType extends object ? ObjectEntries<BaseType>
: never;
BaseType extends Map<unknown, unknown> ? MapEntries<BaseType>
: BaseType extends Set<unknown> ? SetEntries<BaseType>
: BaseType extends unknown[] ? ArrayEntries<BaseType>
: BaseType extends object ? ObjectEntries<BaseType>
: never;

0 comments on commit 670b561

Please sign in to comment.