- Enum khai báo tập hợp named constants.
- Numeric enum tự động assign 0,1,2... có reverse lookup nhưng gây footgun (Direction[0] = 'Up').
- String enum cần gán tường minh, dễ debug hơn, không có reverse lookup.
typescript
enum Direction { Up, Down } // numeric: Up=0, Down=1
Direction[0] // "Up" — reverse lookup
enum Status { Active = 'ACTIVE', Inactive = 'INACTIVE' } // string
// Lưu ý 2024+: TS community khuyến nghị dùng const object thay enum
// vì tree-shaking tốt hơn và không sinh runtime code thừa
const Direction = { Up: 'up', Down: 'down' } as const;
type Direction = typeof Direction[keyof typeof Direction];- Enums declare a set of named constants.
- Numeric enums auto-assign 0, 1, 2... and support reverse lookup, but reverse lookup is a common footgun.
- String enums require explicit string values, are easier to debug, and have no reverse lookup.
typescript
enum Direction { Up, Down } // numeric: Up=0, Down=1
Direction[0] // "Up" — reverse lookup
enum Status { Active = 'ACTIVE', Inactive = 'INACTIVE' } // string
// Note 2024+: TS community recommends const objects over enums
// for better tree-shaking and no runtime overhead
const Direction = { Up: 'up', Down: 'down' } as const;
type Direction = typeof Direction[keyof typeof Direction];