til/typescript string manipulation types

In version 4.1 TypeScript added a set of types which can be used for string manipulation. These types are great for remapping string types to other formats. The types are:

  • Uppercase<StringType>
  • Lowercase<StringType>
  • Capitalize<StringType>
  • Uncapitalize<StringType>

These types are intrinsic, which means that they are built-in to the compiler and we can't create our own implementation of them.

Intrinsic types and their operations are predefined and always accessible.
Their implementations are provided by the TypeScript compiler.

// Creating a uppercase type from another string type
type Greeting = "Hello, world"
type ShoutyGreeting = Uppercase<Greeting>
// type ShoutyGreeting = "HELLO, WORLD"

// Creating a uppercase type with string interpolation
type ASCIICacheKey<Str extends string> = `ID-${Uppercase<Str>}`
type MainID = ASCIICacheKey<"my_app">
// type MainID = "ID-MY_APP"

// Creates a union if the property has multiple values
type Keys = 'name' | 'address'
type Getters = `get${Capitalize<Keys>}`
// type Getters = "getName" | "getAddress"

// Can be used with multiple unions
type Keys = 'name' | 'address'
type Accessors = 'get' | 'set'
type Methods = `${Accessors}${Capitalize<Keys>}`
// type Methods = "getName" | "getAddress" | "setName" | "setAddress"

  • Mark Dalgleish. (2022-01-31). Tweet
  • TypeScript documentation. (2022-01-31). Intrinsic String Manipulation Types. Link
  • Jose Granja. (2021-01-27). TypeScript 4.1’s Advanced Mapped Types. Link
  • Volodymyr Hudyma. (2021-05-04). Intrinsic String Manipulation Types In TypeScript Link