0 of 6 problems solved0%
5 of 6

Levels with as const

String literals widen to string unless you freeze them. as const keeps exact literals and makes arrays/objects read-only. From a literal array, you can build a union of its items.

You will define a set of log levels using as const and use that union in a function.

const COLORS = ["red", "green", "blue"] as const;
type Color = (typeof COLORS)[number];

Your Task

Build levels with as const and tag messages.

  • Declare const LEVELS = ['info','warn','error'] as const.
  • Create type Level = typeof LEVELS[number].
  • Implement tag(level: Level, msg: string): string that returns '[LEVEL] ' + msg with LEVEL uppercased.
  • Keep the function pure.