Lesson 6 of 8
Template Literal Types
Template literal types build new string literal types from existing ones - by combining string literals with type interpolation. They model naming conventions, event names, CSS properties, API paths, and any string pattern at the type level without runtime overhead.
Template Literal Types - String Pattern Safety
// ── Basic template literal type ─────────────────────────────
type EventName = "click" | "focus" | "blur";
type EventHandler = `on${Capitalize<EventName>}`;
// "onClick" | "onFocus" | "onBlur"
// ── Intrinsic string manipulation types ──────────────────────
type Upper = Uppercase<"hello">; // "HELLO"
type Lower = Lowercase<"WORLD">; // "world"
type Cap = Capitalize<"title">; // "Title"
type Uncap = Uncapitalize<"Name">; // "name"
// ── CSS property types ────────────────────────────────────────
type Side = "top" | "right" | "bottom" | "left";
type MarginKey = `margin-${Side}`;
// "margin-top" | "margin-right" | "margin-bottom" | "margin-left"
// ── API route typing ──────────────────────────────────────────
type ApiVersion = "v1" | "v2";
type Resource = "tasks" | "users" | "projects";
type ApiRoute = `/api/${ApiVersion}/${Resource}`;
// "/api/v1/tasks" | "/api/v1/users" | ... | "/api/v2/projects"
// ── Event system with template types ─────────────────────────
type TaskEventName =
| `task:${"created" | "updated" | "deleted" | "completed"}`
| `user:${"joined" | "left" | "promoted"}`;
// "task:created" | "task:updated" | "task:deleted" | "task:completed"
// | "user:joined" | "user:left" | "user:promoted"
// ── Getter naming convention enforcement ──────────────────────
type GetterName<T extends string> = `get${Capitalize<T>}`;
type SetterName<T extends string> = `set${Capitalize<T>}`;
type TaskField = "title" | "status" | "priority";
type TaskGetterNames = GetterName<TaskField>;
// "getTitle" | "getStatus" | "getPriority"
// ── infer with template literals - parsing strings ────────────
type ExtractEventType<S extends string> =
S extends `${infer Prefix}:${infer Event}`
? { prefix: Prefix; event: Event }
: never;
type Parsed = ExtractEventType<"task:completed">;
// { prefix: "task"; event: "completed" }
// ── Practical: typed object key access via string paths ───────
// (simplified - see conditional types for recursive version)
type DotPrefix<T extends string, P extends string> = `${P}.${T}`;
type Prefixed = DotPrefix<"name" | "email", "user">;
// "user.name" | "user.email"Common Mistakes
- Building overly large template literal unions - if you combine 5 unions of 10 members each with template literals, TypeScript must compute 100,000 combinations. It will slow down or error. Keep unions small when using in template literal types.
- Mixing value strings with type strings - template literal TYPES are compile-time only. You cannot 'run' a template literal type to produce a string at runtime. They purely shape what string literals are accepted.
- Not using TypeScript's built-in string utilities - `Uppercase`, `Lowercase`, `Capitalize`, `Uncapitalize` are available globally. No need to re-implement string casing transformations.
Tip
Tip
Practice Template Literal Types in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and concepts for Template Literal Types
Practice Task
Note
Practice Task - (1) Write a working example of Template Literal Types from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Template Literal Types is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready typescript code.
Key Takeaways
- Template literal types build new string literal types from existing ones - by combining string literals with type interpolation.
- Building overly large template literal unions - if you combine 5 unions of 10 members each with template literals, TypeScript must compute 100,000 combinations. It will slow down or error. Keep unions small when using in template literal types.
- Mixing value strings with type strings - template literal TYPES are compile-time only. You cannot 'run' a template literal type to produce a string at runtime. They purely shape what string literals are accepted.
- Not using TypeScript's built-in string utilities - `Uppercase`, `Lowercase`, `Capitalize`, `Uncapitalize` are available globally. No need to re-implement string casing transformations.
Finished this lesson?