Last active
November 30, 2021 02:20
-
-
Save kira-m/7fb4d0bec507dd9f1932fb4d58efcccf to your computer and use it in GitHub Desktop.
Typescript Generic: Convert snake_case to CamelCase
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 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