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:
- Template literal types — `
type Hello =hello ${World}``` - Uppercase<S> — `
Uppercase<"abc"> = "ABC"` - Lowercase<S> — `
Lowercase<"ABC"> = "abc"` - Capitalize<S> — `
Capitalize<"abc"> = "Abc"` - 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
// 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
- TypeScript Advanced Types
String types.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.