sailens
sailens

Reputation: 1614

Declaring enum in a class

I am having trouble declaring an enum element in a class. I've tried several values to declare the enum but I can't get it to work.

This is the (non-working) class:

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;
    }
}

I have also tried:

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

and

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

also

protected numbers:enum{one, two three};

and

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

Not a single one seems to work. So I must be missing something, because at this point I can't understand how enum works. (I have already looked at typescript documentation and several sites for more info without success)

Upvotes: 0

Views: 86

Answers (1)

TSV
TSV

Reputation: 7641

You are going to pass value of 'numbersEnumType' as 3rd parameter to the constructor, so 'numbersEnumType' can not be local type declaration:

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;
    }
}

You can use short variant of declaration:

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

Upvotes: 1

Related Questions