Created
December 9, 2021 14:06
-
-
Save Tsugami/27881b7f9e7b84e511fe915408cfaad6 to your computer and use it in GitHub Desktop.
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
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