Bird
Raised Fist0
Typescriptprogramming~10 mins

When enums add unnecessary runtime code in Typescript - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a string enum with two colors.

Typescript
enum Colors {
  Red = [1],
  Blue = "blue"
}
Drag options to blanks, or click blank then click option'
A'red'
Bred
CRed
D"red"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around string values.
Using variable names instead of string literals.
2fill in blank
medium

Complete the code to define a union type instead of an enum for colors.

Typescript
type Colors = [1];
Drag options to blanks, or click blank then click option'
Aenum { red, blue }
B"red" | "blue"
C["red", "blue"]
D"red", "blue"
Attempts:
3 left
💡 Hint
Common Mistakes
Using enum keyword instead of union type.
Using array syntax instead of union.
3fill in blank
hard

Fix the error in the code by replacing the enum with a union type to avoid runtime code.

Typescript
enum Status {
  Active,
  Inactive
}

function checkStatus(status: [1]) {
  return status === "Active";
}
Drag options to blanks, or click blank then click option'
A"Active" | "Inactive"
BStatus
Cstring
Dnumber
Attempts:
3 left
💡 Hint
Common Mistakes
Using the enum type which is numeric by default.
Using plain string type which is too broad.
4fill in blank
hard

Fill both blanks to create a union type and a function that accepts only those values.

Typescript
type Direction = [1];

function move(dir: Direction): void {
  console.log("Moving", dir);
}

move([2]);
Drag options to blanks, or click blank then click option'
A"up" | "down" | "left" | "right"
B"up"
C"forward"
D"left"
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a string not in the union.
Using enum instead of union type.
5fill in blank
hard

Fill all three blanks to rewrite the enum as a union type and use it in a function with a default parameter.

Typescript
type Status = [1];

function setStatus(status: Status = [2]): string {
  return `Status is ${status}`;
}

console.log(setStatus([3]));
Drag options to blanks, or click blank then click option'
A"online" | "offline" | "away"
B"online"
C"offline"
D"away"
Attempts:
3 left
💡 Hint
Common Mistakes
Using enum instead of union type.
Default value not in the union.
Function call uses invalid status.

Practice

(1/5)
1. What is a main reason to avoid using regular enums in TypeScript when you only need type checking?
easy
A. They add extra runtime code that may be unnecessary.
B. They cause syntax errors in modern TypeScript versions.
C. They cannot be used with functions.
D. They do not support string values.

Solution

  1. Step 1: Understand what regular enums generate

    Regular enums create extra JavaScript code that runs when your program runs, even if you only want type checks.
  2. Step 2: Identify the impact of extra runtime code

    This extra code can increase bundle size and slow down execution unnecessarily if you only need compile-time type safety.
  3. Final Answer:

    They add extra runtime code that may be unnecessary. -> Option A
  4. Quick Check:

    Extra runtime code = Avoid regular enums when only type checks needed [OK]
Hint: Remember: regular enums add runtime code, const enums don't [OK]
Common Mistakes:
  • Thinking enums cause syntax errors
  • Believing enums can't hold string values
  • Assuming enums can't be used with functions
2. Which syntax correctly declares a const enum in TypeScript to avoid runtime code?
easy
A. enum Colors { Red, Green, Blue }
B. const Colors = enum { Red, Green, Blue }
C. const enum Colors { Red, Green, Blue }
D. enum const Colors { Red, Green, Blue }

Solution

  1. Step 1: Recall the correct const enum syntax

    The correct way to declare a const enum is by placing the keyword const before enum like const enum Colors { ... }.
  2. Step 2: Identify incorrect options

    enum Colors { Red, Green, Blue } is a regular enum, not const. const Colors = enum { Red, Green, Blue } and D have invalid syntax order or structure.
  3. Final Answer:

    const enum Colors { Red, Green, Blue } -> Option C
  4. Quick Check:

    const enum syntax = const enum Name { ... } [OK]
Hint: const enum always starts with 'const enum' keywords [OK]
Common Mistakes:
  • Placing const after enum
  • Using invalid syntax like 'const Colors = enum'
  • Confusing regular enum with const enum
3. What will be the output of this TypeScript code when compiled and run in JavaScript?
const enum Status { Active, Inactive }
const current = Status.Active;
console.log(current);
medium
A. Status.Active
B. 0
C. undefined
D. Error at runtime

Solution

  1. Step 1: Understand const enum compilation

    Const enums are inlined by the compiler, so Status.Active becomes the numeric value 0 directly in the output JavaScript.
  2. Step 2: Predict the console output

    Since current is assigned Status.Active, which is 0, the console.log prints 0.
  3. Final Answer:

    0 -> Option B
  4. Quick Check:

    const enum values inline as numbers [OK]
Hint: const enum values become numbers directly in output [OK]
Common Mistakes:
  • Expecting enum object at runtime
  • Thinking it prints the enum name
  • Assuming runtime error due to const enum
4. Identify the problem in this TypeScript code snippet that uses a regular enum:
enum Direction { Up, Down }
function move(dir: Direction) {
  console.log(dir);
}
move(Direction.Left);
medium
A. The function move should return a value.
B. Enums cannot be used as function parameters.
C. The enum must be declared as const enum.
D. Direction.Left does not exist, causing a compile error.

Solution

  1. Step 1: Check enum members

    The enum Direction only has Up and Down. Left is not defined.
  2. Step 2: Understand the error cause

    Using Direction.Left causes a compile-time error because Left is not a valid member.
  3. Final Answer:

    Direction.Left does not exist, causing a compile error. -> Option D
  4. Quick Check:

    Using undefined enum member = compile error [OK]
Hint: Use only defined enum members to avoid errors [OK]
Common Mistakes:
  • Assuming enums can't be function parameters
  • Thinking const enum is required here
  • Expecting runtime error instead of compile error
5. You want to define a set of string values for user roles without adding runtime code. Which TypeScript approach is best?
hard
A. Use a union of string literal types.
B. Use a regular enum with string values.
C. Use a const enum with string values.
D. Use a class with static string properties.

Solution

  1. Step 1: Understand runtime code from enums

    Regular enums with string values generate runtime code. Const enums with string values are not allowed in TypeScript.
  2. Step 2: Identify the best alternative

    Using a union of string literal types (e.g., 'admin' | 'user' | 'guest') provides type safety without any runtime code.
  3. Final Answer:

    Use a union of string literal types. -> Option A
  4. Quick Check:

    String unions avoid runtime code unlike enums [OK]
Hint: Use string unions for roles to skip runtime code [OK]
Common Mistakes:
  • Trying to use const enum with strings (not allowed)
  • Choosing regular enums causing extra code
  • Using classes adds unnecessary complexity
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard