Created
August 21, 2023 21:02
-
-
Save pyldin601/ceaef1eebd539720be3a884c69a25bfc to your computer and use it in GitHub Desktop.
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
/** | |
* 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