objectEntries
A type-safe wrapper around
Object.entries()
that preserves the exact types of object keys and values. Unlike the standard
Object.entries() which returns [string, any][], this function returns an
array of tuples where each tuple is precisely typed according to the input
object's structure.
This is particularly useful when working with objects that have known, fixed property types and you want to maintain type safety when iterating over entries.
@example
const myObject = { a: 1, b: 'hello', c: true } as const
const entries = objectEntries(myObject)
// Type: (["a", 1] | ["b", "hello"] | ["c", true])[]
for (const [key, value] of entries) {
// key is typed as "a" | "b" | "c"
// value is typed as 1 | "hello" | true
console.log(`${key}: ${value}`)
}
@example
interface User {
name: string
age: number
active: boolean
}
const user: User = { name: 'Alice', age: 30, active: true }
const entries = objectEntries(user)
// Type: (["name", string] | ["age", number] | ["active", boolean])[]