Siddhant DevalAuthor
Senior Full-Stack Engineer·Aug 27, 2026·11 min read
Template Literal Types & String Manipulation
Template literal types make TypeScript aware of string structure — enabling type-safe event systems, route contracts, and CSS property validation without any runtime overhead. They are the mechanism underpinning the ergonomics of tRPC, Prisma, and ts-pattern.
Technical Series
TypeScript Mastery
Part 6 of 11
Template Literal Types & String Manipulation
Types are a specification language — not an annotation layer. Template literal types are the proof that this applies to strings, not just objects. TypeScript can be aware that
"/users/:id/posts/:postId" contains two extractable parameters of type string. It can enforce that your event handler map has exactly onClick, onFocus, and onBlur — no more, no less. It does all of this at compile time, with zero runtime overhead.1. Template Literal Type Syntax
Template literal types use the same backtick syntax as JavaScript template strings, but at the type level:
typescript
When unions are substituted, the template distributes to produce a cross-product:
typescript

Expand
2. Built-in String Manipulation Types
TypeScript ships four intrinsic types that transform string literal types. These are compiler primitives — they cannot be implemented in user-land TypeScript:
| Type | Transforms |
|---|---|
Uppercase<S> | "hello" → "HELLO" |
Lowercase<S> | "HELLO" → "hello" |
Capitalize<S> | "hello" → "Hello" |
Uncapitalize<S> | "Hello" → "hello" |
typescript
3. Typing Event Handler Maps
This is the canonical real-world use case for template literal types — generating a complete, exhaustive event handler interface from a union of event names:
typescript
Architectural Note
string & K is required because keyof can produce string | number | symbol. Intersecting with string narrows to just the string keys, which is required for template literal substitution.![ Flow diagram. Left box:
DOMEventMap = { click: MouseEvent; focus: FocusEvent; ... }. Center box shows the mapped type with key remapping: [K in keyof DOMEventMap as \on${Capitalize<string & K>}`]. Right box: EventHandlers = { onClick: (e: MouseEvent) => void; onFocus: (e: FocusEvent) => void; ... }. Arrows trace: 'click'→Capitalize→'Click'→ prepend'on'→'onClick'`. Caption: 'Template literal key remapping auto-generates a complete handler map from an event source-of-truth — adding an event to the map automatically adds the handler'.](/assets/blog/frontend/typescript-template-literal-types/fig-02.png)4. Route Parameter Extraction with infer
Combining template literal types with
infer enables extracting named parameters from route strings — the core of tRPC and Prisma's type ergonomics:typescript

Expand
5. CSS Custom Properties — Type-Safe var()
typescript
6. How tRPC, Prisma, and ts-pattern Use Template Literals
These libraries appear to have magical type inference. The mechanism is always the same: template literal types combined with
infer:- tRPC: Router keys are merged as template literals (
"user.getById","post.create") — the dot-separated structure is parsed at the type level withinferto route calls to the correct procedure type. - Prisma: Model field accessor patterns (
"user_name","post_title") are generated as template literal types from the schema introspection. ts-pattern: The.with(P.string.includes('hello'))pattern DSL uses branded template literal types to carry constraint information through the pattern language.
Architectural Note
These libraries demonstrate that template literal types are not a niche feature — they are the mechanism enabling the modern TypeScript library ergonomics developers take for granted. Understanding them puts you in a position to build similar APIs.
Summary
| Concept | Rule |
|---|---|
| Template literal type | `Hello, ${string}` — type-level string pattern; backtick syntax, compile-time only |
| Cross-product distribution | Union substitution produces all combinations — N × M members for two N- and M-member unions |
Uppercase / Capitalize / etc. | Compiler intrinsics — cannot be implemented in user-land TypeScript |
string & K | Required to use keyof results in template literals — keyof may include number / symbol |
infer in template literals | Extracts string segments by position — powers route param extraction |
| Recursive extraction | Combine template literals + infer recursively to extract all params from a path |
| Zero runtime cost | Template literal type checking happens entirely at compile time — no runtime overhead |
What's Next
In Part 7, we covertsconfigas Architecture — each compiler flag is a type-level design decision with direct runtime safety implications. We decodestrict's seven sub-flags,exactOptionalPropertyTypes,noUncheckedIndexedAccess,verbatimModuleSyntax, and project references for monorepo builds.
Research & Synthesis Note
This article was developed with AI-assisted deep search, specification cross-referencing, and technical research synthesis.
#TypeScript#Template Literal Types#String Manipulation#Advanced TypeScript#Type Safety