Created
June 1, 2022 12:40
-
-
Save danielroe/6d85333ef26b85aa2ac6618213b7c10b to your computer and use it in GitHub Desktop.
fromEntries typing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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