我想遍历一个枚举。当我遍历该枚举时,我会得到键和值,但是我只想通过键将技能实例添加到技能数组中。
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
}
我需要枚举的反向映射。
最佳答案
我猜您已经尝试了一下但无济于事:
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));
demo