对象字面量中的枚举

fub*_*bbe 0 typescript

我希望 TheWall 在 TileSpecs 文字中为 0,但事实并非如此。为什么以及如何解决这个问题?

enum Tile = {WALL, BORDER}
const TheWall: Tile = Tile.WALL;

console.log(TheWall) // Prints 0

let TileSpecs = {
  TheWall: {prop: 'value'}
}

console.log(TileSpecs) // Prints TheWall: {...} 
Run Code Online (Sandbox Code Playgroud)

Joh*_*yHK 5

要将TheWall常量用作属性名称,您需要使用计算属性名称语法:

let TileSpecs = {
  [TheWall]: {prop: 'value'}
}
Run Code Online (Sandbox Code Playgroud)