Template literal types நீங்கள் மற்ற வகைகளை template ல் interpolate செய்வதன் மூலம் புதிய string literal வகைகளை உருவாக்க அனுமதிக்கின்றன — 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 API களை type-safe ஆக்குகின்றன: route params, event systems, CSS-in-JS, ORM column names.
எந்த string ஐயும் ஏற்பதற்குப் பதிலாக, நீங்கள் ஒரு precise, generated செல்லுபடியான strings களின் set க்கு restrict செய்யலாம் — இது paste நேரத்தில் typos ஐ catch செய்கிறது stringly-typed இருந்த இடங்களில்.