Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有更好的方法用typescript模拟枚举?_Typescript - Fatal编程技术网

有没有更好的方法用typescript模拟枚举?

有没有更好的方法用typescript模拟枚举?,typescript,Typescript,我正在创建类似于基类/不同文件中的枚举的内容: ExamStatusId = { All: { elem: this.examStatusSelectId, // << refers to a string text: 'Exam Status: All', val: 0 }, Current: { elem: this.examStatusSelectId, // << refers

我正在创建类似于基类/不同文件中的枚举的内容:

ExamStatusId = {
    All: {
        elem: this.examStatusSelectId, // << refers to a string
        text: 'Exam Status: All',
        val: 0
    },
    Current: {
        elem: this.examStatusSelectId, // << refers to a string
        text: 'Exam Status: Current',
        val: 1
    }
}
下面是另一个类/文件中的函数:

page.isSelectedValue(page.ExamStatusId.All);
isSelectedValue (data) {
    var title = this.getTitle(data.id);
    var valueString = data.val.toString();
    it('Check for ' + data.text, function () {
        expect(data.elem.getAttribute("value")).toBe(valueString);
    });
}
这段代码可以工作,但是有人能告诉我这是否是一种更好的方法,可以使用Typescript将所需的数据传递给
isSelectedValue
函数吗?我已经在代码中使用了Typescript,并希望充分利用它提供的所有功能


另外,我如何确保传递给该函数的内容包含elem、text和val的所有参数?

Typescript具有内置的枚举语法:

enum EnumName {
    member1,
    member2,
    member3
}
EnumName随后成为命名类型,因此您可以拥有一个函数:

((foo: EnumName) => { })(EnumName.member1);
为了满足您的具体要求,您可以采取以下措施:

enum Status {
    All,
    Current
}
ExamStatusID = {
    elem: this.examStatusSelectId
    val: (status: Status) => {
        switch(status) {
            case Status.All: //etc
        }
    }
}

Typescript具有内置的枚举语法:

enum EnumName {
    member1,
    member2,
    member3
}
EnumName随后成为命名类型,因此您可以拥有一个函数:

((foo: EnumName) => { })(EnumName.member1);
为了满足您的具体要求,您可以采取以下措施:

enum Status {
    All,
    Current
}
ExamStatusID = {
    elem: this.examStatusSelectId
    val: (status: Status) => {
        switch(status) {
            case Status.All: //etc
        }
    }
}

看起来您需要下面的代码。 至少在我看来,您似乎没有使用枚举,而是更像是对象常量? 这可以通过一个额外的接口来完成。这方面的好处是,在isSelectedValue函数中仍然有类型检查

interface IElement {
    elem: string;
    text: string;
    val: number
}
var ExamStatusId = {
    All: <IElement>{
        elem: this.examStatusSelectId, // << refers to a string
        text: 'Exam Status: All',
        val: 0
    },
    Current: <IElement>{
        elem: this.examStatusSelectId, // << refers to a string
        text: 'Exam Status: Current',
        val: 1
    }
}

function isSelectedValue (data: IElement) {
}

isSelectedValue(ExamStatusId.All);
接口元素{
元素:字符串;
文本:字符串;
瓦尔:号码
}
变量ExamStatusId={
全部:{

elem:this.examStatusSelectId,//看起来您需要下面的代码。 至少在我看来,您似乎没有使用枚举,而是更像是对象常量? 这是可以通过一个额外的接口来完成的。这方面的好处是,在isSelectedValue函数中仍然有类型检查

interface IElement {
    elem: string;
    text: string;
    val: number
}
var ExamStatusId = {
    All: <IElement>{
        elem: this.examStatusSelectId, // << refers to a string
        text: 'Exam Status: All',
        val: 0
    },
    Current: <IElement>{
        elem: this.examStatusSelectId, // << refers to a string
        text: 'Exam Status: Current',
        val: 1
    }
}

function isSelectedValue (data: IElement) {
}

isSelectedValue(ExamStatusId.All);
接口元素{
元素:字符串;
文本:字符串;
瓦尔:号码
}
变量ExamStatusId={
全部:{

elem:this.examStatusSelectId、//枚举有时在获取相同值时非常繁琐,下面是我在这种情况下使用的内容

  export class urlMapper {
    //contains names of the api url's
    static homeUrl:string = '/home';
    static loginUrl:string ='/auth';
    static userUrl:string ='/user';
    static logoutUrl:string ='/logout';
    static storeUrl:string="/store";

}

枚举有时在获取相同的值时非常繁琐,下面是我在这种情况下使用的

  export class urlMapper {
    //contains names of the api url's
    static homeUrl:string = '/home';
    static loginUrl:string ='/auth';
    static userUrl:string ='/user';
    static logoutUrl:string ='/logout';
    static storeUrl:string="/store";

}

“枚举”对你来说是什么?因为如果你想要一个合适的ADT(对我来说是“真实的”枚举),那么不,Typescript的类型系统不能真正做到这一点。“枚举”对你来说是什么?因为如果你想要一个合适的ADT(对我来说是“真实的”枚举)那么不,Typescript的类型系统不能真正做到这一点。你能再给我一个例子,说明我如何使用它来满足我的需求吗?很抱歉,我对Typescript了解不多。谢谢你能再给我一个例子,说明我如何使用它满足我的需求。很抱歉,我对Typesc了解不多谢谢