enum என்பது பெயரிடப்பட்ட மாறிலிகளின் தொகுப்பை வரையறுக்கிறது. TypeScript-ல் எண் enums மற்றும் string enums உள்ளன.
{ , , , }
.;
[];
{
= ,
= ,
}
.;
enum என்பது பெயரிடப்பட்ட மாறிலிகளின் தொகுப்பை வரையறுக்கிறது. TypeScript-ல் எண் enums மற்றும் string enums உள்ளன.
{ , , , }
.;
[];
{
= ,
= ,
}
.;
বেশিரभाग TypeScript வகைகளைப் போலல்லாமல் (இவை கம்பைல் நேரத்தில் மறைந்துவிடும்), வழக்கமான enum உங்கள்번들ாக JavaScript பொருளை வெளியீடு செய்கிறது. அதனால்தான் பல குழுக்கள் string literals இன் union ஐக் as const உடன் விரும்புகின்றன, இது தூய வகை-நிலை:
// often preferred — zero runtime cost, easy to read in logs
const Status = { Active: "ACTIVE", Inactive: "INACTIVE" } as const;
type Status = typeof Status[keyof typeof Status]; // "ACTIVE" | "INACTIVE"
// or simply:
type Direction = "up" | "down" | "left" | "right";
const enum E { A, B } // inlined at compile time — no runtime object, but has tooling caveats
Enums நிश்চிত தொகுப்புகளுக்கு (states, roles, directions) அর்த்தம் உள்ள பெயர்களை வழங்குகிறது மற்றும் அவற்றை ஒரு namespace இன் கீழ் குழுப்படுத்துகிறது.
ஆனால் அவை இயக்க நேரக் குறியீட்டை உத்பன்னம் செய்வதாலும் மற்றும் விசித்திரங்களைக் கொண்டிருப்பதாலும் (எண் enums தளர்வாகச் சரிபார்க்கப்படுகின்றன), நவீன TypeScript பெரும்பாலும் string-literal unions ஐ எளிய περιπτώσεகளுக்கு விரும்புகிறது — அவை இலகுவாகவும் இயல்பாகவும் தொடர்பு கொள்ளுகின்றன.
Namspacing அல்லது reverse mapping விரும்பினால் enums ஐப் பயன்படுத்தவும்.