Created
September 30, 2021 11:28
-
-
Save ypresto/d07333a03bbcfda577cd7f52f776bd00 to your computer and use it in GitHub Desktop.
Typed uppercase/lowercase/capitalize/uncapitalize function for use with template literal types.
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
/** | |
* For Template Literal Types. Don't forget to add 'as const' after `...` . | |
*/ | |
export function typedUppercase<S extends string>(str: S) { | |
return str.toUpperCase() as Uppercase<S> | |
} | |
/** | |
* For Template Literal Types. Don't forget to add 'as const' after `...` . | |
*/ | |
export function typedLowercase<S extends string>(str: S) { | |
return str.toLowerCase() as Lowercase<S> | |
} | |
/** | |
* For Template Literal Types. Don't forget to add 'as const' after `...` . | |
*/ | |
export function typedCapitalize<S extends string>(str: S) { | |
return str.replace(/^./, c => c.toUpperCase()) as Capitalize<S> | |
} | |
/** | |
* For Template Literal Types. Don't forget to add 'as const' after `...` . | |
*/ | |
export function typedUncapitalize<S extends string>(str: S) { | |
return str.replace(/^./, c => c.toLowerCase()) as Uncapitalize<S> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment