Skip to content

Instantly share code, notes, and snippets.

@kira-m
Last active November 30, 2021 02:20
Show Gist options
  • Save kira-m/7fb4d0bec507dd9f1932fb4d58efcccf to your computer and use it in GitHub Desktop.
Save kira-m/7fb4d0bec507dd9f1932fb4d58efcccf to your computer and use it in GitHub Desktop.
Typescript Generic: Convert snake_case to CamelCase
interface SnakeCasedType {
property_a: string;
property_b: string;
}
type SnakeToCamelCase<S extends string> =
S extends `${infer T}_${infer U}`
? `${Lowercase<T>}${Capitalize<SnakeToCamelCase<U>>}`
: S
type SnakeToCamelCaseNested<T> = T extends object ? {
[K in keyof T as SnakeToCamelCase<K & string>]: SnakeToCamelCaseNested<T[K]>
} : T
type CamelCased = SnakeToCamelCaseNested<SnakeCasedType>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment