Capitalize<T> for First Character Only
What you'll learn: How Capitalize<T> transforms only the first character of a string literal type to uppercase, leaving the rest untouched.
The Concept
TypeScript's Capitalize<T> utility type is a surgical tool—it takes a string literal type and returns a new type with only the first character capitalized. Unlike Uppercase<T>, which transforms the entire string, Capitalize<T> targets just that leading character.
Think of it like a formal greeting: you capitalize someone's name at the start of a sentence, but you don't SHOUT their entire name.
How It Works
type Example1 = Capitalize<"hello">; // "Hello"
type Example2 = Capitalize<"world">; // "World"
type Example3 = Capitalize<"typescript">; // "Typescript"
The transformation is simple:
- First character: converted to uppercase
- All remaining characters: left exactly as they were
type MixedCase = Capitalize<"javaScript">; // "JavaScript"
type AlreadyCap = Capitalize<"Hello">; // "Hello"
type Empty = Capitalize<"">; // ""
Notice that if the string is already capitalized, nothing changes. If it's empty, it remains empty.
Practical Applications
Capitalize<T> shines when building type-safe string transformations:
type EventName = "click" | "focus" | "blur";
type HandlerName = `on${Capitalize<EventName>}`;
// "onClick" | "onFocus" | "onBlur"
This pattern is common in component libraries where event handler names follow a naming convention—the type system ensures consistency without manual typing.
Key Takeaway: Capitalize<T> transforms only the first character of a string literal type to uppercase, making it perfect for title-casing or creating camelCase patterns in template literal types.