在课堂上声明枚举

时间:2015-10-29 14:37:28

标签: typescript typescript1.6

我无法在类中声明枚举元素。我已经尝试了几个值来声明枚举,但我无法让它工作。

这是(非工作)类:

export class Device extends Electronics {
    public OS:string = '';
    protected ready:boolean = false;
    protected enum numbers{one, two, three} 

    constructor(OS:string, ready:boolean, numbers:enum){
        this.OS = OS;
        this.ready = ready;
        this.numbers = numbers;
    }
}

我也尝试过:

protected {one, two, three}numbers:enum;

protected numbers{one, two, three}:enum;

protected numbers:enum{one, two three};

protected numbers:enum = {one, two, three};

似乎没有一个人工作。所以我必须遗漏一些东西,因为在这一点上我无法理解枚举是如何工作的。 (我已经查看过typescript文档和几个网站了解更多信息但没有成功)

1 个答案:

答案 0 :(得分:1)

您将传递' numbersEnumType'的值。作为构造函数的第3个参数,所以' numbersEnumType'不能是本地类型声明:

enum numbersEnumType {one, two, three};

class Device {
    public OS: string = '';
    protected ready: boolean = false;
    protected numbers: numbersEnumType;

    constructor(OS: string, ready: boolean, numbers: numbersEnumType) {
        this.OS = OS;
        this.ready = ready;
        this.numbers = numbers;
    }
}

您可以使用简短的声明变体:

class Device {
    constructor(public OS: string = '', protected ready: boolean = false, protected numbers: numbersEnumType) {
    }
}