I am attempting to define a type called ObjectFromEntries
that functions similarly to the return type of the Object.fromEntries
function. Here is what I have so far:
type ObjectFromEntries<Entries extends [keyof any, any][]> = { [key in Entries[number][0]]: ___ };
declare var z: ObjectFromEntries<[["a", number], ["b", string], ["c", Date]]>
z.a // expected: number
z.b // expected: string
z.c // expected: Date
However, I am unsure what to substitute for ___
.
When I attempt to use
key extends Entries[infer I][0] ? Entries[I][1] : never
, I encounter an error:
Type 'I' cannot be used to index type 'Entries'.(2536)
Type '0' cannot be used to index type 'Entries[I]'.(2536)
If I try Entries[number][1]
, every property ends up with the type string | number | Date
, which makes sense but is not ideal.
How can I effectively synchronize key
with the index to obtain the correct type?