TypeScript · Middle · Question 16 of 75
Why is an as const object often used instead of an enum?
const Status = { Active: 'active', Banned: 'banned' } as const;
type Status = typeof Status[keyof typeof Status];- ✓Correct answer: It's a plain JS object, and the value type is a union of strings
- 2
as construns dozens of times faster at runtime than an enum - 3Enums were removed in TypeScript 5 and no longer compile
Explanation
The object is standard JS: it can be iterated and is compatible with erasable syntax mode (Node.js type stripping). The type is a string union that also accepts literals from an API. Numeric enums, by contrast, accept any number and create a reverse mapping. Enums weren't removed.