Template literal types ఇతర types ను template లో interpolate చేయడం ద్వారా కొత్త string literal types ను నిర్మించడానికి మిమ్మల్ని అనుమతిస్తాయి — type స్థరంలో string manipulation.
= ;
: = ;
: = ;
type Color = "red" | "blue";
type Shade = "light" | "dark";
type Variant = `${Shade}-${Color}`;
// "light-red" | "light-blue" | "dark-red" | "dark-blue" — all combinations
కంపైలర్ unions యొక్క ప్రతి combination ను విస్తరిస్తుంది — CSS classes, event names, route patterns వంటి చెల్లుబాటు అయ్యే string keys ను స్వయంచాలకంగా ఉత్పత్తి చేయడానికి ఉపయోగకరంగా ఉంటుంది.
type Entity = "user" | "post";
type Event = `${Entity}:${"created" | "deleted"}`;
// "user:created" | "user:deleted" | "post:created" | "post:deleted"
function on(event: Event, cb: () => void) {}
on("user:created", () => {}); // ✅
on("user:updated", () => {}); // ❌ not a valid event
type Getters<T> = {
[K in keyof T & string as `get${Capitalize<K>}`]: () => T[K];
};
// { name: string } → { getName: () => string }
Uppercase, Lowercase, Capitalize, Uncapitalize built-in intrinsic helpers.
Template literal types string-based APIs ను type-safe చేస్తాయి: route params, event systems, CSS-in-JS, ORM column names.
కోई string ను అంగీకరించడానికి బదులుగా, మీరు ఒక precise, generated చెల్లుబాటు అయ్యే strings సెట్కు పరిమితం చేయవచ్చు — compile time వద్ద typos ను catch చేస్తుంది stringly-typed ఉన్న ప్రదేశాలలో.