Hone logo
Hone
Problems

Type-Level String Manipulation in TypeScript

TypeScript's powerful type system allows us to perform computations at compile time, enabling a new class of abstractions that are both safe and efficient. This challenge focuses on creating type-level functions to manipulate strings, demonstrating how to build complex type transformations using conditional types and template literal types.

Problem Description

Your task is to implement several type-level functions that operate on string literal types. These functions should mimic common string operations but operate entirely within the TypeScript type system. This is useful for scenarios like generating type-safe API client code, enforcing string formats, or creating dynamic type definitions based on string inputs.

You will implement the following type-level functions:

  1. CapitalizeString<S extends string>: Capitalizes the first character of a string literal type.
  2. LowercaseString<S extends string>: Converts a string literal type to lowercase.
  3. UppercaseString<S extends string>: Converts a string literal type to uppercase.
  4. ReverseString<S extends string>: Reverses a string literal type.
  5. StartsWith<S extends string, Prefix extends string>: Returns true if S starts with Prefix, otherwise false.
  6. EndsWith<S extends string, Suffix extends string>: Returns true if S ends with Suffix, otherwise false.

You should use template literal types and conditional types extensively.

Examples

Example 1: CapitalizeString

type MyString = "hello world";
type Capitalized = CapitalizeString<MyString>; // Expected: "Hello world"

Example 2: ReverseString

type MyString = "typescript";
type Reversed = ReverseString<MyString>; // Expected: "tpircsepyt"

Example 3: StartsWith and EndsWith

type ProtocolHttp = "http";
type Host = "example.com";
type Url = `${ProtocolHttp}://${Host}`; // "http://example.com"

type StartsWithHttp = StartsWith<Url, "http://">; // Expected: true
type StartsWithHttps = StartsWith<Url, "https://">; // Expected: false
type EndsWithCom = EndsWith<Url, ".com">; // Expected: true
type EndsWithOrg = EndsWith<Url, ".org">; // Expected: false

Example 4: UppercaseString and LowercaseString

type MixedCase = "MiXeDcAsE";
type Upper = UppercaseString<MixedCase>; // Expected: "MIXEDCASE"
type Lower = LowercaseString<MixedCase>; // Expected: "mixedcase"

Constraints

  • All implementations must be purely type-level. No runtime JavaScript code should be used for the transformations.
  • The StartsWith and EndsWith functions should return a boolean literal type (true or false).
  • Assume input strings will consist of alphanumeric characters and common punctuation. No need to handle complex Unicode characters or emoji for this challenge.
  • Performance is not a critical concern for this challenge, but strive for readable and maintainable type definitions.

Notes

  • TypeScript's built-in string manipulation types (Uppercase, Lowercase, Capitalize, Uncapitalize) are not to be used directly for the implementation of the core logic of your functions. You should reimplement their behavior using template literal types and conditional types.
  • For ReverseString, you might find it helpful to break the string down recursively or consider iterative approaches within the type system.
  • Consider how to handle empty strings as input for each function.
Loading editor...
typescript