Hone logo
Hone
Problems

Capitalize Type in TypeScript

This challenge focuses on creating a utility type in TypeScript that capitalizes the first letter of a string type. This is a common requirement in various applications, such as generating titles, formatting data for display, or creating consistent naming conventions. Successfully implementing this type will demonstrate your understanding of TypeScript's advanced type manipulation capabilities.

Problem Description

You are tasked with creating a TypeScript type called Capitalize. This type should take a string type as input and return a new type where the first character of the input string type is capitalized. The capitalization should be case-insensitive, meaning both lowercase and uppercase first letters should be correctly capitalized. The rest of the string should remain unchanged.

Key Requirements:

  • The Capitalize type must accept a string type as its argument.
  • The resulting type must be a string type with the first character capitalized.
  • The solution should work correctly regardless of the initial case of the first character (lowercase or uppercase).
  • The solution should handle empty strings gracefully (returning an empty string type).

Expected Behavior:

Given a string type like "hello", the Capitalize<"hello"> type should resolve to "Hello". Given "world", it should resolve to "World". Given "HELLO", it should resolve to "HELLO". Given an empty string "", it should resolve to "".

Edge Cases to Consider:

  • Empty strings: The type should handle empty strings without errors and return an empty string type.
  • Strings with non-ASCII characters: While not strictly required, consider how your solution might behave with Unicode characters. The focus is on the first character, regardless of its encoding.
  • Strings with numbers or symbols: The type should still capitalize the first character, even if it's not a letter.

Examples

Example 1:

Input: "hello"
Output: "Hello"
Explanation: The first character 'h' is converted to 'H', and the rest of the string remains unchanged.

Example 2:

Input: "world"
Output: "World"
Explanation: The first character 'w' is converted to 'W', and the rest of the string remains unchanged.

Example 3:

Input: ""
Output: ""
Explanation: An empty string remains an empty string.

Example 4:

Input: "HELLO"
Output: "HELLO"
Explanation: The first character 'H' is already capitalized, so it remains unchanged.

Constraints

  • The solution must be a valid TypeScript type definition.
  • The solution should be concise and readable.
  • The solution should not rely on external libraries or complex type manipulations beyond what is reasonably expected for a TypeScript type definition.
  • The solution should be compatible with TypeScript 4.0 or later.

Notes

Consider using conditional types and string manipulation techniques within your type definition. Think about how to extract the first character of the string type and then reconstruct the string with the capitalized first character. The Uppercase utility type might be helpful, but be mindful of how it interacts with the rest of the string. Remember that TypeScript types are erased at runtime, so you're not actually modifying strings; you're defining a type-level transformation.

Loading editor...
typescript