import { type Entry } from "https://dotland.deno.dev/x/scaffold@0.3.0/src/deps/types.ts";
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.
For example the Object
, Map
, Array
, and Set
collections all have this method. Note that WeakMap
and WeakSet
do not have this method since their entries are not enumerable.
Examples
Example 1
Example 1
import type {Entry} from 'type-fest';
interface Example {
someKey: number;
}
const manipulatesEntry = (example: Entry<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(),
];
const example: Example = {someKey: 1};
const entry = Object.entries(example)[0] as Entry<Example>;
const output = manipulatesEntry(entry);
// Objects
const objectExample = {a: 1};
const objectEntry: Entry<typeof objectExample> = ['a', 1];
// Arrays
const arrayExample = ['a', 1];
const arrayEntryString: Entry<typeof arrayExample> = [0, 'a'];
const arrayEntryNumber: Entry<typeof arrayExample> = [1, 1];
// Maps
const mapExample = new Map([['a', 1]]);
const mapEntry: Entry<typeof mapExample> = ['a', 1];
// Sets
const setExample = new Set(['a', 1]);
const setEntryString: Entry<typeof setExample> = ['a', 'a'];
const setEntryNumber: Entry<typeof setExample> = [1, 1];