如何以Typescript 0.9.5编程枚举枚举类型? [英] How to programmatically enumerate an enum type in Typescript 0.9.5?

查看:156
本文介绍了如何以Typescript 0.9.5编程枚举枚举类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个TypeScript枚举,MyEnum,如下所示:

 枚举MyEnum {
首先,
第二,
第三
}

什么是最好的方式TypeScript 0.9.5生成枚举值的数组?示例:

  var choices:MyEnum []; //或Array< MyEnum> 
choices = MyEnum.GetValues(); //计划这个?
choices = EnumEx.GetValues(MyEnum); //或者,怎么滚我自己?


解决方案

这是该枚举的JavaScript输出: p>

  var MyEnum; 
(function(MyEnum){
MyEnum [MyEnum [First] = 0] =First;
MyEnum [MyEnum [Second] = 1] =Second
MyEnum [MyEnum [Third] = 2] =Third;
})(MyEnum ||(MyEnum = {}));

这是一个像这样的对象:

  Object {
0:First,
1:Second,
2:Third,
First:0,
第二:1,
第三:2
}

strong>成员名称



要获取枚举成员名称,我们可以通过任何是字符串过滤对象的值:

  const objValues = Object.keys(MyEnum).map(k => MyEnum [k]); 
const names = objValues.filter(v => typeof v ===string)as string [];

包含: [First,Second,Third]



会员价值



枚举成员值,我们可以通过是数字的任何东西过滤对象的值:

  const值= objValues.filter(v => typeof v ===number)as number []; 

包含: [0,1,2]



扩展类



我认为最好的方法是创建自己的函数(例如 EnumEx.getNames(MyEnum))。您不能向枚举添加一个函数。

  class EnumEx {
static getNamesAndValues< T extends number>( e:any){
return EnumEx.getNames(e).map(n =>({name:n,value:e [n] as T}));
}

static getNames(e:any){
return EnumEx.getObjValues(e).filter(v => typeof v ===string)as string [];
}

static getValues< T extends number>(e:any){
return EnumEx.getObjValues(e).filter(v => typeof v ===数字)作为T [];
}

private static getObjValues(e:any):(number | string)[] {
return Object.keys(e).map(k => e [ K]);
}
}


Say I have a TypeScript enum, MyEnum, as follows:

enum MyEnum {
    First,
    Second,
    Third
}

What would be the best way in TypeScript 0.9.5 to produce an array of the enum values? Example:

var choices: MyEnum[]; // or Array<MyEnum>
choices = MyEnum.GetValues(); // plans for this?
choices = EnumEx.GetValues(MyEnum); // or, how to roll my own?

解决方案

This is the JavaScript output of that enum:

var MyEnum;
(function (MyEnum) {
    MyEnum[MyEnum["First"] = 0] = "First";
    MyEnum[MyEnum["Second"] = 1] = "Second";
    MyEnum[MyEnum["Third"] = 2] = "Third";
})(MyEnum || (MyEnum = {}));

Which is an object like this:

Object {
    0: "First",
    1: "Second",
    2: "Third",
    First: 0,
    Second: 1,
    Third: 2
}

Member Names

To get the enum member names, we can filter the object's values by anything that is a string:

const objValues = Object.keys(MyEnum).map(k => MyEnum[k]);
const names = objValues.filter(v => typeof v === "string") as string[];

Contains: ["First", "Second", "Third"]

Member Values

While to get the enum member values, we can filter the object's values by anything that is a number:

const values = objValues.filter(v => typeof v === "number") as number[];

Contains: [0, 1, 2]

Extension Class

I think the best way to do this is to create your own functions (ex. EnumEx.getNames(MyEnum)). You can't add a function to an enum.

class EnumEx {
    static getNamesAndValues<T extends number>(e: any) {
        return EnumEx.getNames(e).map(n => ({ name: n, value: e[n] as T }));
    }

    static getNames(e: any) {
        return EnumEx.getObjValues(e).filter(v => typeof v === "string") as string[];
    }

    static getValues<T extends number>(e: any) {
        return EnumEx.getObjValues(e).filter(v => typeof v === "number") as T[];
    }

    private static getObjValues(e: any): (number | string)[] {
        return Object.keys(e).map(k => e[k]);
    }
}

这篇关于如何以Typescript 0.9.5编程枚举枚举类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆