我想创建一个以枚举为成员的元组。
enum Category {
Medieval,
Fantasy,
SciFi,
Pirates
}
let games: [string, string, boolean, enum] = [
{ name: 'Star Wars: Imperial Assault', company: 'Fantasy Flight', available: true, category: Category.SciFi },
{ name: 'Game of Thrones: Second Edition', company: 'Fantasy Flight', available: false, category: Category.Fantasy },
{ name: 'Merchans and Marauders', company: 'Z-Man Gaming', available: true, category: Category.Pirates },
{ name: 'Eclipse', company: 'lautapelit', available: false, category: Category.SciFi }
]
VisualStudio在元组中的枚举值返回错误
预期输入
答案 0 :(得分:1)
试试这个
enum Category {
Medieval,
Fantasy,
SciFi,
Pirates
}
function getAllGames() {
let games: {name:string, company:string, available:boolean, category:Category}[] = [
{ name: 'Star Wars: Imperial Assault', company: 'Fantasy Flight', available: true, category: Category.SciFi },
{ name: 'Game of Thrones: Second Edition', company: 'Fantasy Flight', available: false, category: Category.Fantasy },
{ name: 'Merchans and Marauders', company: 'Z-Man Gaming', available: true, category: Category.Pirates },
{ name: 'Eclipse', company: 'lautapelit', available: false, category: Category.SciFi }
]
// de functie GetAllGames returned de let array 'games' zodat deze array ergens anders gebruikt kan worden.
return games;
}
答案 1 :(得分:0)
实际上要使用元组来执行此操作,看起来像这样...
enum Category {
Medieval,
Fantasy,
SciFi,
Pirates
}
let games: [string, string, boolean, Category][] = [
[ 'Star Wars: Imperial Assault', 'Fantasy Flight', true, Category.SciFi ],
[ 'Game of Thrones: Second Edition', 'Fantasy Flight', false, Category.Fantasy ],
[ 'Merchans and Marauders', 'Z-Man Gaming', true, Category.Pirates ],
[ 'Eclipse', 'lautapelit', false, Category.SciFi ]
];
您也可以在声明“游戏”之前将元组类型删除,以使其更简洁...
type Game = [string, string, boolean, Category];
然后写
let games: Game[] = [
// Games go here
];