Question presented to candidate:
"What are enums in TypeScript, and would you use them in a new codebase? Compare numeric enums, string enums, const enum, and a union of string literals or an as const object."
What a strong answer should cover:
- An enum is one of the few TypeScript features that emits run-time code: a real object, and for numeric enums a reverse mapping from number to name
- Numeric enums auto-increment and accept numbers; since 5.0 an out-of-range literal is rejected, but any non-literal
numberstill is not - String enums have no reverse mapping and are opaque: the raw string
"ACTIVE"is not assignable toStatus, and two enums with the same values are not interchangeable const enumis inlined by a fulltscbuild but not by single-file transpilers, andisolatedModulesforbids ambient const enums- Alternatives: a union of string literals (zero run-time cost) or an
as constobject plus a derived union (named constants, easy iteration, run-time validation) - Trade-offs today:
erasableSyntaxOnly(TypeScript 5.8) and Node type stripping reject enums because they are not erasable syntax
Code / implementation expected: Optional β define a string enum and the equivalent as const object, and show how each is used and iterated.