迭代枚举,将enum枚举元素作为参数

时间:2018-05-26 13:03:42

标签: javascript typescript enums

我想迭代一个枚举。当我迭代这个枚举时,我得到了键和值,但我只想用键来将技能的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
}

我需要枚举的反向映射。

1 个答案:

答案 0 :(得分:0)

我猜你已经尝试过这个无济于事了:

skills.push(new Skill(skill)); // Error: Argument of type 'string' is not assignable to parameter of type 'eSkills'.

您可以使用type assertionas eSkills):

来解决这个问题
skills.push(new Skill(skill as eSkills));

demo

相关问题