Object
Helper functions that have to do with objects.
Functions
Section titled “Functions”getObjectEntries()
Section titled “getObjectEntries()”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 Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
T extends object |
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
object |
T |
Returns
Section titled “Returns”readonly { [K in string | number | symbol]: [K, T[K]] }[keyof T][]
getObjectKeys()
Section titled “getObjectKeys()”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 Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
T extends object |
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
object |
T |
Returns
Section titled “Returns”readonly keyof T[]
getWidenedObjectValue()
Section titled “getWidenedObjectValue()”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 Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
T extends Record<string, unknown> |
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
object |
T |
key |
WidenLiteral<keyof T> |
Returns
Section titled “Returns”T[keyof T] | undefined
objectFilter()
Section titled “objectFilter()”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 Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
K extends PropertyKey |
V |
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
object |
ReadonlyRecord<K, V> |
predicate |
(value) => boolean |
Returns
Section titled “Returns”readonly V[]
objectMap()
Section titled “objectMap()”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 Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
K extends PropertyKey |
V |
U |
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
object |
Record<K, V> |
callback |
(key, value) => U |
Returns
Section titled “Returns”ReadonlyRecord<K,
U>
objectToMap()
Section titled “objectToMap()”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 Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
K extends PropertyKey |
V |
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
object |
Record<K, V> |
Returns
Section titled “Returns”ReadonlyMap<K, V>
objectToReverseMap()
Section titled “objectToReverseMap()”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 Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
K extends PropertyKey |
V extends PropertyKey |
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
object |
Record<K, V> |
Returns
Section titled “Returns”ReadonlyMap<V, K>
reverseObject()
Section titled “reverseObject()”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 Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
K extends PropertyKey |
V extends PropertyKey |
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
obj |
Record<K, V> |
Returns
Section titled “Returns”ReadonlyRecord<V,
K>
Throws
Section titled “Throws”If the object has duplicate values.
