Skip to content

Instantly share code, notes, and snippets.

@sergeyt
Created February 27, 2019 07:39
Show Gist options
  • Save sergeyt/3feea098528dbcfa1d2610b7d163e7e9 to your computer and use it in GitHub Desktop.
Save sergeyt/3feea098528dbcfa1d2610b7d163e7e9 to your computer and use it in GitHub Desktop.
generic map object
function mapObject<K extends keyof any, T, TResult>(obj: Record<K, T>, makeResult: (val: T, key?: K) => TResult): Record<K, TResult> {
return (Object.keys(eventMap) as K[]).reduce(
(acc, key) => ({
...acc,
[key]: makeResult(obj[key]),
}),
{} as Record<K, TResult>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment