Const enums help make your code faster and smaller by replacing enum values directly in the code.
Const enums and optimization in Typescript
const enum EnumName { VALUE1, VALUE2 = 5, VALUE3 }
Const enums are declared with the const enum keywords.
The compiler replaces enum uses with their actual values, so no enum object exists at runtime.
Direction.Up will be replaced by 0 in the compiled code.const enum Direction { Up, Down, Left, Right } let move = Direction.Up;
Status.Success will be replaced by 1 in the output.const enum Status { Success = 1, Failure = 0 } let result = Status.Success;
This program uses a const enum Colors. When compiled, Colors.Green is replaced by 1 directly, so the output is the number 1.
const enum Colors { Red, Green, Blue } function printColor(color: Colors) { console.log(color); } printColor(Colors.Green);
Const enums cannot be used with computed members.
They are removed during compilation, so you cannot get their keys or values at runtime.
Use const enums only when you do not need the enum object at runtime.
Const enums replace enum references with actual values to optimize code.
They reduce runtime code size and improve speed.
Use them for fixed sets of constants when you don't need enum objects at runtime.
Practice
const enum in TypeScript?Solution
Step 1: Understand what
const enumdoesconst enumreplaces enum references with their actual values during compilation, so no enum object exists at runtime.Step 2: Compare runtime behavior
Unlike regular enums,const enumdoes not generate extra JavaScript code for the enum object, reducing code size and improving speed.Final Answer:
It replaces enum references with actual values at compile time, reducing runtime code. -> Option CQuick Check:
Const enums optimize code by replacing references with values [OK]
- Thinking const enums create runtime objects
- Believing const enums allow runtime modification
- Confusing const enums with regular enums
Solution
Step 1: Recall the correct keyword order
The correct syntax uses the keywordconstbeforeenumto declare a const enum.Step 2: Check each option
const enum Colors { Red, Green, Blue } matches the correct syntax:const enum Colors { Red, Green, Blue }. Options C and D use invalid keywords or order.Final Answer:
const enum Colors { Red, Green, Blue } -> Option BQuick Check:
Const enum syntax = const enum [OK]
- Writing 'constant' instead of 'const'
- Placing 'const' after 'enum'
- Omitting 'const' keyword
const enum Direction {
Up = 1,
Down,
Left,
Right
}
const move = Direction.Left;
console.log(move);Solution
Step 1: Understand enum value assignment
The enum starts withUp = 1, soDown = 2,Left = 3, andRight = 4.Step 2: Identify the value of
It is 3, soDirection.Leftmoveis assigned 3, andconsole.log(move)prints 3.Final Answer:
3 -> Option AQuick Check:
Enum Left value = 3 [OK]
- Assuming Left is 2 instead of 3
- Expecting string output 'Left'
- Confusing enum with object property
const enum Status {
Active = 1,
Inactive = 0
}
console.log(Status[1]);Solution
Step 1: Understand const enum limitations
Const enums do not generate an object at runtime, so reverse mapping (accessing by index) is not supported.Step 2: Identify the error cause
UsingStatus[1]tries to access reverse mapping, causing a compile-time error.Final Answer:
Compile-time error: Cannot use reverse mapping with const enums -> Option AQuick Check:
Reverse mapping disallowed in const enums [OK]
- Expecting reverse mapping to work with const enums
- Thinking it outputs string 'Active'
- Ignoring compile-time errors
Solution
Step 1: Recall const enum behavior
Const enums inline values and do not create runtime objects, so reverse mapping (index to name) is not available.Step 2: Analyze each option
Options A, B, and D work fine as they use the inlined values directly. Accessing enum values by their numeric index (reverse mapping). requires reverse mapping, which fails with const enums.Final Answer:
Accessing enum values by their numeric index (reverse mapping). -> Option DQuick Check:
Reverse mapping breaks with const enums [OK]
- Assuming all enum features work with const enums
- Using reverse mapping without error
- Confusing const enum with regular enum behavior
Ready to prove it?
Pass the test and this pattern moves unknown
