Enum vs Union Types

Enum Vs Union
πŸ‘¨β€πŸ’Ό You've already practiced string enums. Modern TypeScript often prefers string literal unions for the same job. The starter includes an enum-based logWithEnum as a reference; add a union-based logging function next to it.
🐨 Open
index.ts
and:
  1. Leave the existing LogLevel enum and logWithEnum function in the file as a reference
  2. Add logWithUnion(level, message) that accepts the same four levels as a string literal union ('debug' | 'info' | 'warn' | 'error') instead of the enum type, and logs in the same [LEVEL] message style
  3. Export logWithUnion by name (you do not need to export the enum version)

Completion criteria

  • Named export: logWithUnion
  • Calling logWithUnion with 'debug' | 'info' | 'warn' | 'error' and a message string runs without throwing
  • The parameter type is a string literal union (not the LogLevel enum)
If you see an "experimental transform-types" warning when running this step, that's from the workshop runtime and can be ignored.

Please set the playground first

Loading "Enum vs Union Types"
Loading "Enum vs Union Types"