Home > Enterprise >  Assign enum value to variable
Assign enum value to variable

Time:12-30

I have an issue, why this is not possible and how to solve it

enum Cars {
    FORD='Ford',
    KIA='KIA'
}

class MyCars {
    public carName: typeof Cars =Cars;
}

const car1 = new MyCars();
car1.carName = Cars.FORD;

CodePudding user response:

Your carName typing is incorrect. Your code fixed:

enum Cars {
    FORD='Ford',
    KIA='KIA'
}

class MyCars {
    public carName?: Cars;
}

const car1 = new MyCars();
car1.carName = Cars.FORD;

TypeScript playground

  • Related