Enums in TypeScript: Numeric, String and Const Enums with Compiled Output
Follow an order status from TypeScript to emitted JavaScript. Calculate numeric members, read reverse mappings and choose between ordinary enums, const enums and literal unions.
KnowledgeGate Team
Exam prep & CS education

An order's status should not depend on unexplained numbers or strings that someone can misspell. TypeScript offers numeric enums, string enums, const enums and literal unions to express these choices. An order example makes the runtime trade-offs visible by tracing what survives compilation into JavaScript.
What an enum adds to a TypeScript program
An enum gives related constants a name. You can use that name as a type and access its members as values:
enum OrderStatus { Pending, Paid, Shipped }
const current: OrderStatus = OrderStatus.Paid;
console.log(current); // 1Here, Pending, Paid and Shipped are 0, 1 and 2. The annotation says what current accepts; OrderStatus.Paid supplies the value. The quoted string "Paid" is not the same expression.
An ordinary enum creates runtime JavaScript. A type alias disappears after checking. Keep that distinction in mind while reading the output below. If annotations are new, start with TypeScript for Beginners: Add Safe Types to JavaScript.
Numeric enums: calculate every member value
Replace the earlier declaration with this example. Run it separately rather than combining both declarations in one file:
enum OrderStatus {
Pending = 10,
Paid,
Shipped = 20,
Delivered
}
console.log(OrderStatus.Paid);
console.log(OrderStatus.Delivered);
console.log(OrderStatus[11]);Calculate in declaration order:
Member | Calculation | Value |
|---|---|---|
Pending | Explicit initializer | 10 |
Paid | 10 + 1 | 11 |
Shipped | Explicit initializer | 20 |
Delivered | 20 + 1 | 21 |
The console prints 11, 21 and Paid, in that order. An explicit initializer resets the next increment; an uninitialised first member starts at zero.
For numeric codes saved in a database or sent over an API, assign every value explicitly. Inserting an auto-numbered member can otherwise shift later codes. Enum syntax does not validate incoming data: your application must still check whether an external value represents an allowed status.
Compiled JavaScript and reverse mappings

For an ES2015 target, ordinary enum emission without bundler transformations produces this JavaScript for the declaration. Formatting can differ between compiler versions; the three logging statements remain after it.
var OrderStatus;
(function (OrderStatus) {
OrderStatus[OrderStatus["Pending"] = 10] = "Pending";
OrderStatus[OrderStatus["Paid"] = 11] = "Paid";
OrderStatus[OrderStatus["Shipped"] = 20] = "Shipped";
OrderStatus[OrderStatus["Delivered"] = 21] = "Delivered";
})(OrderStatus || (OrderStatus = {}));Read the Paid line from the inside out. First, OrderStatus["Paid"] = 11 writes the forward property and evaluates to 11. The outer assignment then writes OrderStatus[11] = "Paid". Numeric-looking object keys are strings at runtime, so that reverse property has the key "11".

The wrapper receives an existing object or creates an empty one, then populates it. This is a JavaScript object, not an automatic database or validation table. Notice that enumerating its keys exposes both member names and reverse keys, so those keys are not simply a list of statuses.
String enums: readable values without reverse lookup
Use a separate enum when the stored value should explain itself:
enum OrderLabel {
Pending = "PENDING",
Paid = "PAID",
Shipped = "SHIPPED"
}
console.log(JSON.stringify({ status: OrderLabel.Paid }));
// {"status":"PAID"}Each string member needs a constant initializer, either a string literal or another string enum member. There is no string auto-increment. The declaration emits:
var OrderLabel;
(function (OrderLabel) {
OrderLabel["Pending"] = "PENDING";
OrderLabel["Paid"] = "PAID";
OrderLabel["Shipped"] = "SHIPPED";
})(OrderLabel || (OrderLabel = {}));Trace the serialisation: OrderLabel.Paid reads "PAID", which becomes the value of the JSON status field. The output stays readable without consulting a numeric-code list.
There is no generated mapping from "PAID" back to "Paid". In plain JavaScript, looking up that missing property gives undefined; such a lookup is not valid strict TypeScript against this enum. Renaming a member and changing its serialised value are separate decisions.
Const enums: what gets inlined
Try this independent example:
const enum DeliveryStep { Packed = 1, Dispatched, Delivered }
const trail = [DeliveryStep.Packed, DeliveryStep.Delivered];The values are 1, 1 + 1 = 2 and 2 + 1 = 3. With tsc, preserveConstEnums: false, isolatedModules: false and an ES2015 target, the output is:
const trail = [1 /* DeliveryStep.Packed */, 3 /* DeliveryStep.Delivered */];The declaration is removed and both member accesses become numbers. Build settings can remove the explanatory comments. There is no DeliveryStep object here to enumerate or reverse-map.
Configuration matters: preserveConstEnums retains the enum declaration, so “const enums always disappear” is incorrect. Check the toolchain that actually builds your project.
The Handbook warns that exported ambient const enums can cause isolated-module compatibility and version-skew problems for consumers. Prefer ordinary enums or unions for public interfaces unless you understand the publishing setup and how consumers compile it.
When a union type is the simpler choice
An API accepting three strings can use a literal union:
type StatusText = "PENDING" | "PAID" | "SHIPPED";
const next: StatusText = "PAID"; // accepted
// const wrong: StatusText = "DONE"; // compile-time errorThe emitted declaration is const next = "PAID";. There is no runtime StatusText object. Incoming JSON still needs validation before you treat its status as a checked value.
If you also want named runtime properties, use this independent pattern:
const Status = { Pending: "PENDING", Paid: "PAID" } as const;
type StatusValue = typeof Status[keyof typeof Status];keyof typeof Status selects "Pending" | "Paid"; indexing their values derives exactly "PENDING" | "PAID". The object remains in JavaScript. The type alias and assertion disappear; as const does not call Object.freeze.
Requirement | Suitable choice |
|---|---|
Deliberate numeric codes and reverse lookup | Ordinary numeric enum |
Named runtime constants with readable values | String enum |
Type-only allowed strings | Literal union |
Inlining under controlled compilation | Const enum |
Output questions and the traps they test
These original interview-style checks test whether you can trace values:
enum Check { A = 4, B, C = 9, D }What are Check.B, Check.D and Check[9]?
Check.Bis5, because4 + 1 = 5.Check.Dis10, because9 + 1 = 10.Check[9]is"C", through the reverse mapping.
Now consider this declaration:
enum Broken { A = Math.random(), B }This fails type checking: B needs an initializer after a computed member. It is a compilation error, not a question asking for a random output. Fix it explicitly:
enum Fixed { A = Math.random(), B = 1 }A remains runtime-computed. The compiler rules and const-enum caveats follow the TypeScript Handbook's enum reference. Remember the other traps: string enums lack reverse mappings, and unions produce no runtime object.
The short version and your next step
Numeric enums create forward and reverse properties. String enums keep readable values. Configured const-enum compilation inlines members, while unions constrain strings during checking.
Try changing Pending from 10 to 30 in the numeric example. Paid becomes 30 + 1 = 31; Shipped = 20 and Delivered = 21 remain unchanged.
For a broader web-development next step, explore MERN Stack. To compare wider programming options, browse Coding & Skills. First, practise predicting the JavaScript output before running each example.
Keep learning
Hoisting in JavaScript: Predict Output with var, let, const and Functions
Build a reliable scope-setup model for JavaScript hoisting, then use it to trace declarations, diagnose three common errors and solve output questions.
Enums in C Explained: Values, a Worked Output Trace and Exam Traps
Learn how C assigns enum values, how explicit resets and duplicate constants behave, and how to trace enum-based switch output without missing fall-through.
Storage Classes, Structures and Enums in C: Worked Examples for GATE Output and sizeof Questions
Trace static variables across calls, calculate structure padding byte by byte, compare structures with unions, and follow enum values after an explicit jump.
Compiled vs Interpreted vs JIT Languages: How Code Actually Runs
Trace one program from source to CPU through ahead-of-time compilation, bytecode interpretation and JIT compilation. Then compare their costs without relying on labels.





