Dev Helpers

TypeScript / Template literal types

Represent numeric IDs as strings

Dev Helpers · November 9, 2025

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-42

Implementation 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

Related articles

Reject fractional cent amounts

A numeric type cannot by itself guarantee whole cents. Validate integrality where money enters the typed part of the application.

Infer route names from a registry

A route registry can drive both lookup and allowed route names. Type the keys from the registry so a removed route cannot survive in a separate union.