Skip to content

Instantly share code, notes, and snippets.

@ypresto
Created September 30, 2021 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ypresto/d07333a03bbcfda577cd7f52f776bd00 to your computer and use it in GitHub Desktop.
Save ypresto/d07333a03bbcfda577cd7f52f776bd00 to your computer and use it in GitHub Desktop.
Typed uppercase/lowercase/capitalize/uncapitalize function for use with template literal types.
/**
* 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