Skip to content

Instantly share code, notes, and snippets.

@max10rogerio
Created October 23, 2020 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save max10rogerio/f0296886180d958b514ec28e0a11253d to your computer and use it in GitHub Desktop.
Save max10rogerio/f0296886180d958b514ec28e0a11253d to your computer and use it in GitHub Desktop.
Snake case to CammelCase
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