Skip to content

Instantly share code, notes, and snippets.

@fnimick
Created September 21, 2024 23:57
Show Gist options
  • Save fnimick/24a559436af317374694fe4d2d2046ee to your computer and use it in GitHub Desktop.
Save fnimick/24a559436af317374694fe4d2d2046ee to your computer and use it in GitHub Desktop.
import { Record } from "effect";
function renameKeys<T extends Record<string | symbol, unknown>, M extends Record<string, string>>(
input: T,
map: M,
): { [Property in keyof T as Property extends keyof M ? M[Property] : Property]: T[Property] } {
return Record.mapKeys(input, (key) => (key in map ? map[key as keyof M] : key)) as any;
}
function renameKey<
T extends Record<string | symbol, unknown>,
K extends keyof T,
K2 extends string,
>(input: T, key: K, newKey: K2) {
return renameKeys(input, { [key]: newKey } as Record<K, K2>);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment