#35991Medium

MyUppercase

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.

Challenge Instructions: MyUppercase

Medium

Implement Uppercase<T>, convert all letter to uppercase

Change the following code to make the test cases pass (no type check errors).

ChallengeSolution
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   '>
  >,
]

Pro Challenge

Unlock 102+ medium, hard, and extreme challenges to master advanced TypeScript.

One-time payment. Lifetime access.

Detailed Explanation

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:

This 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.

Share this challenge