我想迭代一个枚举。当我迭代这个枚举时,我得到了键和值,但我只想用键来将技能的Intances添加到技能数组中。
enum eSkills {
ACROBATICS = <any>"Acrobatics",
APPRAISE = <any>"Appraise",
BLUFF = <any>"Bluff",
CLIMB = <any>"Climb",
CRAFT = <any>"Craft"
}
class Skill {
constructor(name: eSkills) {
this.name = name;
}
name: eSkills;
}
let skills: Skill[] = [];
for (let skill in eSkills) {
//TODO create new instance of Skill and push to skills array
}
我需要枚举的反向映射。
答案 0 :(得分:0)
我猜你已经尝试过这个无济于事了:
skills.push(new Skill(skill)); // Error: Argument of type 'string' is not assignable to parameter of type 'eSkills'.
您可以使用type assertion(as eSkills
):
skills.push(new Skill(skill as eSkills));