Skip to content

Instantly share code, notes, and snippets.

@jednano
Last active March 1, 2019 16:23
Show Gist options
  • Save jednano/7eb124e8d84e53a24dfbc4492ad7cc74 to your computer and use it in GitHub Desktop.
Save jednano/7eb124e8d84e53a24dfbc4492ad7cc74 to your computer and use it in GitHub Desktop.
Object.fromEntries in TypeScript
type ObjectPairs<T, X = keyof T, Y = string> = Array<[X, Y]> | Map<X, Y>
export function objectFromEntries<
T extends { [key: string]: any } = { [key: string]: any },
P extends ObjectPairs<T> = ObjectPairs<T>
>(pairs: P) {
const result = {} as T
for (const [key, value] of pairs.entries()) {
result[key] = value
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment