Created
October 23, 2020 13:38
-
-
Save max10rogerio/f0296886180d958b514ec28e0a11253d to your computer and use it in GitHub Desktop.
Snake case to CammelCase
This file contains 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
interface Example { | |
first_name: string, | |
last_name: string, | |
home_town: string, | |
} | |
type CamelizeString<T extends PropertyKey> = T extends string ? string extends T ? string : | |
T extends `${infer F}_${infer R}` ? `${F}${Capitalize<CamelizeString<R>>}` : T : T; | |
type Camelize<T> = { [K in keyof T as CamelizeString<K>]: T[K] } | |
type CamelizeExample = Camelize<Example>; | |
/* type CamelizeExample = { | |
firstName: string; | |
lastName: string; | |
homeTown: string; | |
} */ | |
const e: Camelize<Example> = { | |
firstName: 'string', | |
lastName: 'string', | |
homeTown: 'string' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment