Hone logo
Hone
Problems

TypeScript Lowercase String Type

This challenge focuses on creating a TypeScript utility type that can transform any string literal type into its lowercase equivalent. This is useful for enforcing consistent casing in string-based configurations, enums, or identifiers, preventing subtle bugs that can arise from case sensitivity.

Problem Description

Your task is to create a TypeScript generic type named LowercaseString that takes a string literal type as input and returns a new string literal type where all characters are converted to lowercase.

Key Requirements:

  • The type should be a generic utility type, accepting a single type parameter.
  • It must correctly handle all uppercase letters (A-Z).
  • It should preserve any characters that are not uppercase letters (e.g., numbers, symbols, already lowercase letters).
  • The type should be purely declarative – no runtime code is involved.

Expected Behavior:

If you apply LowercaseString to a string literal type like "HELLO", it should result in "hello". If you apply it to "Hello World 123!", it should result in "hello world 123!".

Edge Cases to Consider:

  • What happens if the input type is an empty string ("")?
  • What happens if the input type contains only non-alphabetic characters?
  • What happens if the input type contains a mix of uppercase, lowercase, and other characters?

Examples

Example 1:

type UppercaseText = "HELLO WORLD";
type LowercaseText = LowercaseString<UppercaseText>;
// Expected Output Type: "hello world"

Example 2:

type MixedCaseText = "TypeScript Is FUN!";
type LowercaseMixed = LowercaseString<MixedCaseText>;
// Expected Output Type: "typescript is fun!"

Example 3:

type NumericString = "12345";
type LowercaseNumeric = LowercaseString<NumericString>;
// Expected Output Type: "12345"

Example 4:

type EmptyString = "";
type LowercaseEmpty = LowercaseString<EmptyString>;
// Expected Output Type: ""

Constraints

  • The solution must be a TypeScript type definition.
  • No runtime JavaScript code should be part of the solution.
  • The solution should leverage built-in TypeScript conditional types and intrinsic string manipulation types.

Notes

TypeScript provides intrinsic string manipulation types that can be incredibly powerful. Think about how you can process a string character by character or leverage existing transformations. You might need to consider how to recursively build the lowercase string if a direct, simple approach isn't immediately obvious.

Success looks like a LowercaseString<T> type that accurately transforms any given string literal type T into its lowercase equivalent.

Loading editor...
typescript