枚举的应用

Posted on Jun 11, 2020

展示TypeScript中的枚举(enum)的一种实际用例。

class P {
  x: number;
  y: number;
  constructor(x: number, y: number) {
    this.x = x;
    this.y = y;
  }
}
enum key {
  x = "x",
  y = "y",
}

const p0 = new P(1, 2);

function dosomething(key: key) {
  console.log(p0[key])
}

dosomething('x');
dosomething('y');