Created
February 12, 2021 15:22
-
-
Save kdmadej/c3e016bcc17f7803abe32d3923ed84f1 to your computer and use it in GitHub Desktop.
TypeScript template string literal type casing
This file contains hidden or 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
| type DefaultSeparator = '_' | '-'; | |
| type CamelCase<S extends string, Separator extends string = DefaultSeparator, Beginning extends boolean = true> | |
| = S extends `${infer Character}${infer Rest}` | |
| ? Character extends Separator | |
| ? `${CamelCase<Capitalize<Rest>, Separator, Beginning>}` | |
| : `${Beginning extends true ? Lowercase<Character> : Character}${CamelCase<Rest, Separator, false>}` | |
| : ''; | |
| // Example: type T = "awaBbbCcc" | |
| type T = | |
| | CamelCase<'Awa_bbb-ccc'> | |
| | CamelCase<'_Awa_bbb-ccc'> | |
| | CamelCase<'-Awa_bbb-ccc'> | |
| ; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This might be a bit computationally expensive ๐ so use at your own discretion ๐
Strings longer than 15 will trigger an error: