Skip to content

Instantly share code, notes, and snippets.

@pyldin601
Created August 21, 2023 21:02
Show Gist options
  • Save pyldin601/ceaef1eebd539720be3a884c69a25bfc to your computer and use it in GitHub Desktop.
Save pyldin601/ceaef1eebd539720be3a884c69a25bfc to your computer and use it in GitHub Desktop.
/**
* Enhanced `toUpperCase` function with improved type inference.
*
* Traditional methods return a general `string` type.
* This version gives a specific uppercase type based on the input.
*
* E.g., for an input type 'hello', this returns type 'HELLO'.
*/
type ToUpperCase<S extends string> = S extends `${infer First}${infer Rest}`
? `${Uppercase<First>}${ToUpperCase<Rest>}`
: S;
export function toUpperCase<S extends string>(str: S): ToUpperCase<S> {
return str.toUpperCase() as ToUpperCase<S>;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment