Skip to content

String Manipulation Types

In one line

TypeScript can transform string types at compile time: uppercase, capitalize, replace, split, template literals.

In simple words

TypeScript can manipulate string types at the type level using:

  1. Template literal types — `type Hello = hello ${World}```
  2. Uppercase<S> — `Uppercase<"abc"> = "ABC"`
  3. Lowercase<S> — `Lowercase<"ABC"> = "abc"`
  4. Capitalize<S> — `Capitalize<"abc"> = "Abc"`
  5. Uncapitalize<S> — `Uncapitalize<"Abc"> = "abc"`

Combined with mapped types and as clauses, you can:

  • Generate getter/setter method names from property names
  • Create event handler types (on${Capitalize<Event>})
  • Transform API response types to client types
  • Parse and validate string formats (URLs, dates)

Example

typescript
// Generate event handler names
type EventHandler<E extends string> = `on${Capitalize<E>}`;
type Handlers = EventHandler<"click" | "change" | "submit">;
// "onClick" | "onChange" | "onSubmit"

// Generate getter methods from properties
type Getters<T> = {
  [K in keyof T as `get${Capitalize<string & K>}`]: () => T[K];
};
type PersonGetters = Getters<{ name: string; age: number }>;
// { getName: () => string; getAge: () => number }

// String path type
type Path<S extends string> = S extends `${infer Head}/${infer Tail}`
  ? Head | Path<Tail>
  : S;
type Parts = Path<"a/b/c">; // "a" | "b" | "c"

Template literal types and built-in string utilities transform string types at compile time. Generate method names, event handlers, and parse paths — all type-level.

Common confusions

  • Confused with: runtime string methods

    The difference: Runtime string methods (.toUpperCase(), .replace()) operate on values at runtime. String manipulation types (Uppercase<S>, template literal types) operate on string literal types at compile time. No runtime code is generated — it's purely type-level.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary