Skip to content

Object

Helper functions that have to do with objects.

function getObjectEntries<T>(
object,
): readonly { [K in string | number | symbol]: [K, T[K]] }[keyof T][];

Defined in: functions/object.ts:16

Helper function to get the keys and values of an object with their literal key types preserved.

This is a typed wrapper around Object.entries. The type assertion is necessary because TypeScript defines the keys from Object.entries as being string[].

Type Parameter
T extends object
Parameter Type
object T

readonly { [K in string | number | symbol]: [K, T[K]] }[keyof T][]


function getObjectKeys<T>(object): readonly (keyof T[]);

Defined in: functions/object.ts:32

Helper function to get keys of an object with their literal key types preserved.

This is a typed wrapper around Object.keys. The type assertion is necessary because TypeScript defines Object.keys as returning string[].

Type Parameter
T extends object
Parameter Type
object T

readonly keyof T[]


function getWidenedObjectValue<T>(object, key): T[keyof T] | undefined;

Defined in: functions/object.ts:44

Safely gets a value from a widened object.

This is useful when normal indexing produces a type error from an object that uses an as const assertion.

Type Parameter
T extends Record<string, unknown>
Parameter Type
object T
key WidenLiteral<keyof T>

T[keyof T] | undefined


function objectFilter<K, V>(object, predicate): readonly V[];

Defined in: functions/object.ts:58

Helper function to get the values in an object that match an arbitrary condition. Similar to the Array.filter method, but works for objects.

This is efficient such that it avoids converting the object values into an array.

Type Parameter
K extends PropertyKey
V
Parameter Type
object ReadonlyRecord<K, V>
predicate (value) => boolean

readonly V[]


function objectMap<K, V, U>(object, callback): ReadonlyRecord<K, U>;

Defined in: functions/object.ts:80

Helper function to map the values in an object to another object with new values. Similar to the Array.map method, but works for objects.

Type Parameter
K extends PropertyKey
V
U
Parameter Type
object Record<K, V>
callback (key, value) => U

ReadonlyRecord<K, U>


function objectToMap<K, V>(object): ReadonlyMap<K, V>;

Defined in: functions/object.ts:103

Helper function to convert an object to a Map.

This is useful when you need to construct a type safe object with the satisfies operator, but then later on you need to query it in a way where you expect the return value to be T or undefined. In this situation, by converting the object to a map, you can avoid unsafe type assertions.

Note that the converted map will only have string keys, due to the nature of JavaScript objects only having string keys under the hood.

Type Parameter
K extends PropertyKey
V
Parameter Type
object Record<K, V>

ReadonlyMap<K, V>


function objectToReverseMap<K, V>(object): ReadonlyMap<V, K>;

Defined in: functions/object.ts:121

Helper function to convert an object to a reverse map.

Note that the converted map will only have string keys, due to the nature of JavaScript objects only having string keys under the hood.

Type Parameter
K extends PropertyKey
V extends PropertyKey
Parameter Type
object Record<K, V>

ReadonlyMap<V, K>


function reverseObject<K, V>(obj): ReadonlyRecord<V, K>;

Defined in: functions/object.ts:139

Helper function to reverse the keys and values of an object.

Type Parameter
K extends PropertyKey
V extends PropertyKey
Parameter Type
obj Record<K, V>

ReadonlyRecord<V, K>

If the object has duplicate values.