Last active
March 1, 2019 16:23
-
-
Save jednano/7eb124e8d84e53a24dfbc4492ad7cc74 to your computer and use it in GitHub Desktop.
Object.fromEntries in TypeScript
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 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