Skip to content

Instantly share code, notes, and snippets.

@danielroe
Created June 1, 2022 12:40
Show Gist options
  • Save danielroe/6d85333ef26b85aa2ac6618213b7c10b to your computer and use it in GitHub Desktop.
Save danielroe/6d85333ef26b85aa2ac6618213b7c10b to your computer and use it in GitHub Desktop.
fromEntries typing
type Entries<T extends Readonly<Array<readonly [string, any]>>> = {
[Index in keyof T]: { [K in T[Index][0]]: T[Index][1] }
}[number]
type UnionToIntersection<U> =
(U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never
const fromEntries = <T extends Readonly<Array<readonly [string, any]>>>(entries: T): UnionToIntersection<Entries<T>> =>
Object.fromEntries(entries) as any
const b = fromEntries([['a', 'foo'], ['b', 42]] as const)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment