An enum mendefinisikan satu set named constants. TypeScript duwe numeric lan string enums.
ts
{ , , , }
.;
[];
{
= ,
= ,
}
.;
An enum mendefinisikan satu set named constants. TypeScript duwe numeric lan string enums.
{ , , , }
.;
[];
{
= ,
= ,
}
.;
Bedane paling TypeScript types (sing ilang ing compile time), regular enum ngirim JavaScript object menyang bundle mu. Mulane akeh tim milih union of string literals plus as const, sing murni type-level:
// 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 menehi jeneng sing meaningful kanggo fixed sets (states, roles, directions) lan nglompokke dheweke ana ing siji namespace.
Nanging amarga enums ngirim runtime code lan duwe quirks (numeric enums iku loosely checked), modern TypeScript asring milih string-literal unions kanggo simple cases — dheweke luwih ringan lan serialize secara natural.
Gunakake enums nalika sampeyan pengin namespacing utawa reverse mapping.