枚举

Tutorial: TS初级一 Category: TS Published: 2026-04-07 13:58:26 Views: 21 Likes: 0 Comments: 0
  1. 枚举举例
enum Direction {
  Up,
  Right,
  Down,
  Left,
}

enum Direction1 {
  Up = 20,
  Right,
  Down,
  Left = 80,
}

enum Direction2 {
  Up = "UP",
  Right = "RIGHT",
  Down = "DOWN",
  Left = "LEFT",
}

function changeDirection(dir: Direction) {
  console.log(dir);
}

function changeDirection1(dir: Direction1) {
  console.log(dir);
}
function changeDirection2(dir: Direction2) {
  console.log(dir);
}

changeDirection(Direction.Down); // 2
changeDirection1(Direction1.Down); // 22
changeDirection1(Direction1.Left); // 80
changeDirection2(Direction2.Left); // LEFT
Prev: 字面量类型 Next: