この解答例はChatGPTによって生成されています,正しいかは自己判断で。

BFE.devでのAI解答例 - TypeScript タイプパズル
51. implement Capitalize<T>

type MyCapitalize<T extends string> = T extends `${infer First}${infer Rest}`
  ? `${Uppercase<First>}${Rest}`
  : T;

Explanation:

  • We define a generic type MyCapitalize<T> that takes a string type T as input.
  • Using conditional types, we check if T is of the form ${infer First}${infer Rest}. If it is, then we know that First is the first character of the string and Rest is the rest of the string.
  • We use the Uppercase<T> built-in utility to convert First to uppercase and then concatenate it with Rest to form the final capitalized string.
  • If T is not of the required form (i.e., it is an empty string, a single character string, or not a string at all), then we return T as is.