示例工程 14-runtime-syntax

14-runtime-syntax/01-enum-output/src/main.ts

typescript · 0.4 KB

运行:14 · enum-output

点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。

// 演示:enum 的运行时产物(对象 + 反向映射)
export enum Color {
  Red,
  Green,
  Blue,
}
 
export enum Direction {
  Up = 'UP',
  Down = 'DOWN',
}
 
export function run(log: (msg: string) => void = console.log): string[] {
  const lines = [
    String(Color.Green),
    Color[1] ?? 'missing',
    Direction.Up,
    String(Object.keys(Color).length),
  ];
  for (const line of lines) {
    log(line);
  }
  return lines;
}