Combining Intrinsics with Template Literals
What you'll learn: How to nest TypeScript's built-in string transformation types inside template literal types to create sophisticated string transformations at the type level.
The Core Idea
You've learned template literals create new string patterns, and the intrinsic types (Uppercase<T>, Lowercase<T>, Capitalize<T>, Uncapitalize<T>) transform strings. Now comes the powerful part: you can nest intrinsics directly inside template literal slots to transform parts of strings as you build them.
Think of it like composing functions in JavaScript—except here you're composing type transformations. Just as you might write toUpperCase(firstName + lastName) in runtime code, you can write Uppercase<{LastName}> at the type level.
The Syntax
type EventName<T extends string> = `on${Capitalize<T>}`;
// "click" → "onClick"
// "change" → "onChange"
type Getter<T extends string> = `get${Capitalize<T>}`;
type Setter<T extends string> = `set${Capitalize<T>}`;
// "name" → "getName", "setName"
type CSSProperty<T extends string> = `--${Lowercase<T>}`;
// "THEME_COLOR" → "--theme_color"
The intrinsic type wraps around the specific part of the template you want to transform, while the rest of the template remains as-is.
Practical Example
type HttpMethod = "get" | "post" | "put" | "delete";
type HandlerName<M extends HttpMethod> = `handle${Capitalize<M>}Request`;
// Expands to:
// "handleGetRequest" | "handlePostRequest"
// | "handlePutRequest" | "handleDeleteRequest"
Key Takeaway: Nesting intrinsics inside template literal types lets you apply precise transformations to specific portions of generated strings, enabling powerful type-safe string pattern generation for APIs, event handlers, and configuration keys.