Lesson 22 — Template literal types (expert)
String types built from pieces
Template literal types use the same backtick syntax as template strings, but for types. With a union inside, TypeScript makes every combination: on${"Click" | "Hover"} is "onClick" | "onHover". Any other string is an error.
type Size = "sm" | "lg";
type ClassName = `btn-${Size}`; // "btn-sm" | "btn-lg"
const c: ClassName = "btn-lg";
Why it matters: event names, CSS classes and routes become exact and autocompleted instead of loose strings.
Define EventName as on + Action, and make handlerName build it. Expect onClick, then onHover.
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
A quick 3-question quiz on this lesson — instant feedback.
Ready for more? Optional paid resources that pick up where this lesson leaves off.
Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.
Project-based TypeScript Foundations courses if you want a structured paid path after the free lessons. Wait for a sale — prices drop often.
Browse on Udemy →Search Amazon for TypeScript Foundations books — check the publication year so the examples match current versions.
Search Amazon →