Skip to content

Instantly share code, notes, and snippets.

@dperuo
Last active May 17, 2024 12:57
Show Gist options
  • Save dperuo/4bf180194243941dc55376e3fdcabb94 to your computer and use it in GitHub Desktop.
Save dperuo/4bf180194243941dc55376e3fdcabb94 to your computer and use it in GitHub Desktop.
/**
* An array of mutable key-value pairs from an object, including optional keys.
*
* @see {@linkcode ReadonlyEntries} for a readonly version.
*
* @example
*
* ```ts
* Entries<{ a: number, b: string }>
* // => (["a", number] | ["b", string])[]
* ```
*/
export type Entries<T> = Array<{ [K in keyof T]-?: [K, T[K]]; }[keyof T]>;
/**
* An array of readonly key-value pairs from an object, including optional keys.
*
* @see {@linkcode Entries} for a mutable version.
*
* @example
*
* ```ts
* ReadonlyEntries<{ a: number, b: string }>
* // => readonly (readonly ["a", number] | readonly ["b", string])[]
* ```
*/
export type ReadonlyEntries<T> = ReadonlyArray<{ [K in keyof T]-?: readonly [K, T[K]]; }[keyof T]>;
/**
* Represents a single entry of type `T`.
*
* @see {@linkcode ReadonlyEntry} for a readonly version.
*
* @example
*
* ```ts
* Entry<{ a: number, b: string }>
* // => ["a", number] | ["b", string]
* ```
*/
export type Entry<T> = Entries<T>[number];
/**
* Represents a readonly entry of type `T`.
*
* @see {@linkcode Entry} for a mutable version.
*
* @example
*
* ```ts
* ReadonlyEntry<{ a: number, b: string }>
* // => readonly ["a", number] | readonly ["b", string]
* ```
*/
export type ReadonlyEntry<T> = ReadonlyEntries<T>[number];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment