Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sepsol/65fb790bfd75047ade88a0c591e6fa67 to your computer and use it in GitHub Desktop.
Save sepsol/65fb790bfd75047ade88a0c591e6fa67 to your computer and use it in GitHub Desktop.
Typescript type camelCase / snake_case conversion
type CamelCase<S extends string> = S extends `${infer P1}_${infer P2}${infer P3}`
? `${Lowercase<P1>}${Uppercase<P2>}${CamelCase<P3>}`
: Lowercase<S>
type KeysToCamelCase<T> = {
[K in keyof T as CamelCase<string & K>]: T[K]
}
type CamelToSnakeCase<S extends string> = S extends `${infer T}${infer U}` ?
`${T extends Capitalize<T> ? "_" : ""}${Lowercase<T>}${CamelToSnakeCase<U>}` :
S
type KeysToSnakeCase<T> = {
[K in keyof T as CamelToSnakeCase<string & K>]: T[K]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment