Hone logo
Hone
Problems

Uppercase String Type in TypeScript

This challenge focuses on creating a custom type in TypeScript that enforces uppercase for string values. This is useful for scenarios where you need to ensure that certain string properties or variables are always stored in uppercase, such as for API keys, constants, or specific configuration settings. You'll be building a type that validates strings at compile time, preventing lowercase or mixed-case values.

Problem Description

You need to create a TypeScript type called UppercaseString. This type should accept a string literal and ensure that the string is entirely in uppercase. If a lowercase or mixed-case string is passed, the TypeScript compiler should throw a type error. The type should not modify the string; it should only validate it.

Key Requirements:

  • Create a type alias named UppercaseString.
  • The type should accept only strings that are entirely uppercase.
  • The type should provide a compile-time error if a lowercase or mixed-case string is used.
  • The type should not transform the string to uppercase; it should only validate.

Expected Behavior:

  • UppercaseString should be assignable to strings that are entirely uppercase.
  • UppercaseString should not be assignable to strings that contain lowercase characters or mixed-case characters. The compiler should flag this as an error.

Edge Cases to Consider:

  • Empty strings: An empty string ("") is considered uppercase and should be valid.
  • Strings with spaces: Strings containing spaces (e.g., "HELLO WORLD") are valid as long as all characters are uppercase.
  • Strings with numbers or special characters: Strings containing numbers or special characters (e.g., "HELLO123", "HELLO_WORLD") are valid as long as all characters are uppercase.

Examples

Example 1:

Input: "HELLO"
Output: Valid UppercaseString
Explanation: The string is entirely in uppercase.

Example 2:

Input: "hello"
Output: Type error: Type '"hello"' is not assignable to type 'UppercaseString'.
Explanation: The string contains lowercase characters.

Example 3:

Input: "Hello"
Output: Type error: Type '"Hello"' is not assignable to type 'UppercaseString'.
Explanation: The string contains mixed-case characters.

Example 4:

Input: ""
Output: Valid UppercaseString
Explanation: An empty string is considered uppercase.

Example 5:

Input: "HELLO WORLD"
Output: Valid UppercaseString
Explanation: The string contains spaces, but all characters are uppercase.

Constraints

  • The solution must be written in TypeScript.
  • The solution must use type aliases.
  • The solution must not use any runtime functions (e.g., toUpperCase()). The validation must be done at compile time.
  • The solution should be concise and readable.

Notes

Consider using conditional types and template literal types to achieve the desired validation. Think about how to check if each character in the string is an uppercase character. Remember that TypeScript's type system operates at compile time, so you're not actually modifying the string; you're defining a type that restricts what values are allowed.

Loading editor...
typescript