0
0
Typescriptprogramming~20 mins

When enums add unnecessary runtime code in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Enum Runtime Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of enum vs union type

What is the output of the following TypeScript code when compiled to JavaScript and run?

enum Colors {
  Red = "RED",
  Green = "GREEN",
  Blue = "BLUE"
}

console.log(typeof Colors);
console.log(Colors.Red);
Typescript
enum Colors {
  Red = "RED",
  Green = "GREEN",
  Blue = "BLUE"
}

console.log(typeof Colors);
console.log(Colors.Red);
A"object"\n"RED"
B"string"\n"RED"
C"undefined"\n"RED"
D"object"\nundefined
Attempts:
2 left
💡 Hint

Remember that enums in TypeScript compile to objects in JavaScript.

Predict Output
intermediate
2:00remaining
Output of union type vs enum

Consider this TypeScript code snippet. What will it output when run in JavaScript?

type Status = "success" | "error" | "loading";

const status: Status = "success";
console.log(status);
Typescript
type Status = "success" | "error" | "loading";

const status: Status = "success";
console.log(status);
A"success"
Bundefined
CReferenceError
D"Status"
Attempts:
2 left
💡 Hint

Union types disappear after compilation and do not add runtime code.

🧠 Conceptual
advanced
2:00remaining
Why enums add runtime code

Why do TypeScript enums add extra runtime code compared to union types?

ABecause enums are replaced by strings at runtime automatically.
BBecause enums compile to JavaScript objects to support reverse mapping and runtime access.
CBecause enums are only compile-time types and do not generate any JavaScript code.
DBecause enums are transpiled to functions that return values.
Attempts:
2 left
💡 Hint

Think about what enums provide at runtime that union types do not.

Predict Output
advanced
2:00remaining
Runtime size difference between enum and union

Given these two TypeScript declarations, what will be the size in bytes of the compiled JavaScript code for each?

enum Directions {
  Up,
  Down,
  Left,
  Right
}

type DirectionsUnion = "Up" | "Down" | "Left" | "Right";
AUnion type code is larger because it stores strings at runtime.
BBoth produce the same size of JavaScript code.
CEnum code is larger due to generated object; union type adds no runtime code.
DEnum code is smaller because it uses numbers only.
Attempts:
2 left
💡 Hint

Consider what code is generated for enums vs union types.

Predict Output
expert
2:00remaining
Output and runtime behavior of const enum vs enum

What is the output and runtime difference when running this code?

enum RegularEnum {
  A = 1,
  B = 2
}

const enum ConstEnum {
  A = 1,
  B = 2
}

console.log(RegularEnum.A);
console.log(ConstEnum.B);
AThrows ReferenceError because ConstEnum is not defined at runtime.
BOutputs 1 and 2; both enums exist as objects at runtime.
COutputs undefined and 2; RegularEnum is undefined at runtime.
DOutputs 1 and 2; RegularEnum exists at runtime, ConstEnum is inlined and does not exist at runtime.
Attempts:
2 left
💡 Hint

Recall how const enums are compiled differently than regular enums.