TypeScript Generics and Advanced Type Operators
Template literal types — compile-time string patterns
A template literal type combines string fragments with interpolated types. Finite unions in interpolations expand into possible combinations, while broader primitive interpolations may describe a pattern. It can support typed event names, key remapping and conditional extraction using infer. Large combinations can become expensive for the checker. A matching static type does not validate an arbitrary runtime string. Example output: "ready:changed". Local strict TypeScript 5.8.3 / ES2022 baseline. The type construct itself is erased; the logged value illustrates a checked use, not runtime type validation.
Formal Syntax & Structure
typescript syntaxtypescript
type EventName = `${"draft" | "ready"}:changed`;Working Example
Example Snippettypescript
type Topic = "draft" | "ready";type EventName = `${Topic}:changed`;const event: EventName = "ready:changed";console.log(event);