Template literal types can restrict which strings are accepted as identifiers. Runtime data still needs validation when it crosses an input boundary.
Worked example
type Area = "jobs" | "users";
type Action = "read" | "write";
console.log(((id: `job-${number}`) => id)("job-42"));Result
job-42Implementation notes
A template literal type is erased from emitted JavaScript. Validate URL parameters or API payloads before asserting that they match it.
Try it yourself
Compare a valid numeric-looking ID with a nonnumeric label at compile time.
Open the TypeScript example →Editorial date November 9, 2025 · Published December 6, 2026