TS中的枚举定字符串

  • 通过给枚举定义字符串可以使用消息管理器
  • 之前都是用静态对象定义
import { EventEmitter } from 'events';

enum EventType {
OPEN = 'open',
CLOSE = 'close',
}

const eventEmitter = new EventEmitter();

eventEmitter.on(EventType.OPEN, () => {
console.log('call open');
});

eventEmitter.on(EventType.CLOSE, () => {
console.log('call close');
});

eventEmitter.emit(EventType.OPEN);
eventEmitter.emit(EventType.CLOSE);