Skip to content

Instantly share code, notes, and snippets.

@Tsugami
Created December 9, 2021 14:06
Show Gist options
  • Save Tsugami/27881b7f9e7b84e511fe915408cfaad6 to your computer and use it in GitHub Desktop.
Save Tsugami/27881b7f9e7b84e511fe915408cfaad6 to your computer and use it in GitHub Desktop.
declare function rename<
P extends string,
P2 extends string,
O extends object,
Key extends keyof O
>(
prefix: P,
newPrefix: P2,
obj: O
): {
[K in Key extends `${P}${infer A}` ? `${P2}${A}` : Key]: O[K extends Key
? K
: K extends `${P2}${infer A}`
? `${P}${A}`
: never];
};
const obj = { str1: 1, str2: 2, a3: "1" };
const result = rename("str", "string", obj);
result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment