Skip to content

Instantly share code, notes, and snippets.

@findscode
Created April 21, 2022 18:21
Show Gist options
  • Save findscode/0dd88be05b7bc7f1ae912674eebe2351 to your computer and use it in GitHub Desktop.
Save findscode/0dd88be05b7bc7f1ae912674eebe2351 to your computer and use it in GitHub Desktop.
TypeScript `CamelCase to UPPER_SNAKE_CASE` type
type CamelToUpperSnake<T extends string, P extends string = ''> = string extends Uncapitalize<T> ? string :
T extends `${infer C0}${infer R}` ?
CamelToUpperSnake<R, `${P}${C0 extends Uppercase<C0> ? '_' : ''}${Uppercase<C0>}`> : P;
// works with both camelCase and CamelCase
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment