Function: objToMap()
objToMap<
T,Key,Value>(input):TypedMap<T>
Defined in: packages/core/src/obj/objToMap.ts:32
Converts an object into a Map with strong, heterogeneous typing. Unlike a standard Map<string, Value>, the returned Map tracks the specific type of each key-value pair based on the input object's structure.
Type Parameters
T
T extends object
The type of the input object.
Key
Key extends string | number | symbol = keyof T
Value
Value = T[Key]
Parameters
input
T
An object to convert. If null or not an object, an empty Map is returned.
Returns
TypedMap<T>
A Map (specifically a TypedMap) populated with the object's own enumerable string properties.
Example
Convert an object to a map
typescript
import { objToMap } from '@superutils/core'
const obj = {
a: 1,
b: false,
c: 'c',
}
const map = objToMap(obj)
const x = map.get('a') // TypeScript knows this is: number | undefined
console.log(x) // Prints: 1
const y = map.get('b') // TypeScript knows this is: boolean | undefined
console.log(y) // Prints: false