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
and:
- Leave the existing
LogLevelenum andlogWithEnumfunction in the file as a reference - 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] messagestyle - Export
logWithUnionby name (you do not need to export the enum version)
Completion criteria
- Named export:
logWithUnion - Calling
logWithUnionwith'debug' | 'info' | 'warn' | 'error'and a message string runs without throwing - The parameter type is a string literal union (not the
LogLevelenum)
If you see an "experimental transform-types" warning when running this step,
that's from the workshop runtime and can be ignored.