`${T}` Template Literal Types

Last updated: 2026-10-22

`${T}` Template Literal Types

Template literal types concatenate at the type level: `${A}-${B}` enumerates every combination of A and B's literals. Ideal for string conventions like event names, routes and CSS classes.

GroupType Composition
KindType
Strict modeNot required

ℹ️ Info The editor transpiles TypeScript to JavaScript and runs it (no type checking); see each example's Output for type-check results.

📝 Syntax

type Lang = "zh" | "en";
type Page = "home" | "about";
type Route = `/${Lang}/${Page}`; // a union of 4 combinations

⚙️ Parameters

`${T}`Place literal unions inside ${}; union members expand into a cartesian product
Uppercase/LowercaseBuilt-in character-transform utility types sharing this mechanism

Returns:A union of every string combination — purely compile-time, nonexistent at runtime.

▶ Example

Edit the code below and click Try it to see the transpiled result:

Output:

user:created | order:deleted

(Runtime output after transpilation; "user:updated" in the array is a compile error — it is not among the 4 combinations)

💡 Notes

ℹ️ Info Placeholders must be literal types (string | number works, but union expansion happens on literal unions); the combination count is the product of member counts.
💡 Tip Pair with infer in conditional types to extract fragments: type OnEvent<T> = T extends `on${infer E}` ? E : never.
⚠️ Note Large unions explode combinatorially (10x10 = 100 branches) and slow compilation; cap union sizes or fall back to string.
More cases coming soon.

📚 Related tutorials

❓ FAQ

Q Do template literal types exist at runtime?
A No. It is a compile-time type operation, erased after transpilation; only ordinary string values exist at runtime.
Q How do I extract part of a string type?
A Conditional types with infer: type Event<T> = T extends `on${infer Name}` ? Name : never turns onCreated into created. See the infer-keyword entry.
Q Are built-ins like Uppercase<T> template literal types?
A Yes — Uppercase/Lowercase/Capitalize/Uncapitalize transform characters via the same mechanism and compose freely with custom template literal types.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏