数字枚举 const枚举

  1. const enum Direction {
  2. Up = 1,
  3. Down,
  4. Left,
  5. Right
  6. }
  7. console.log(Direction['Down']);

反向隐射

  1. let a = Direction.Up
  2. // let b = Direction[a] // Up
  3. console.log(Direction['Left']);

外部枚举

  • 外部枚举用来描述已经存在的枚举类型的形状。

    1. declare enum Enum { A = 1, B, C = 2 }

    类型推论

  • 在有些没有明确指出类型的地方,类型推论会帮助提供类型

    1. let x = 3; // 变量x的类型被推断为数字

    上下文类型

    1. window.onmousedown = function (mEvent:any) {
    2. console.log(mEvent.button);
    3. }