Implement `Uppercase<T>`, convert all letter to uppercase Master advanced TypeScript type manipulation in this medium-level challenge on TypeScriptPro.
In this medium-level challenge, you'll implement your own version of the built-in Uppercase<T> intrinsic type, converting all lowercase letters in a string literal type to uppercase without using the built-in.
Implement Uppercase<T>, convert all letter to uppercase
Change the following code to make the test cases pass (no type check errors).
type cases = [
Expect<Equal<MyUppercase<'a'>, 'A'>>,
Expect<Equal<MyUppercase<'Z'>, 'Z'>>,
Expect<
Equal<MyUppercase<'A z h yy 😃cda\n\t a '>, 'A Z H YY 😃CDA\n\t A '>
>,
]
Unlock 102+ medium, hard, and extreme challenges to master advanced TypeScript.
One-time payment. Lifetime access.
type UpperMap = {
a: 'A'; b: 'B'; c: 'C'; d: 'D'; e: 'E'; f: 'F'; g: 'G';
h: 'H'; i: 'I'; j: 'J'; k: 'K'; l: 'L'; m: 'M'; n: 'N';
o: 'O'; p: 'P'; q: 'Q'; r: 'R'; s: 'S'; t: 'T'; u: 'U';
v: 'V'; w: 'W'; x: 'X'; y: 'Y'; z: 'Z';
};
type MyUppercase<T extends string> =
T extends `${infer First}${infer Rest}`
? `${First extends keyof UpperMap ? UpperMap[First] : First}${MyUppercase<Rest>}`
: T;How it works:
${infer First}${infer Rest}First is a key in UpperMap (i.e., a lowercase letter from a to z)Rest until the string is empty, at which point the base case returns the empty string T\n and \t, so they pass through without special handlingThis challenge helps you understand recursive template literal type transformations and character-level string processing, and how to apply these concepts in real-world scenarios.
This challenge is originally from here.