typescript check if value is in enum 1

typescript check if value is in enum

Enum Vehicle {
    Car = 'car',
    Bike = 'bike',
    Truck = 'truck'
}

// becomes:

{
    Car: 'car',
    Bike: 'bike',
    Truck: 'truck'
}

// So you just need to do:

if (Object.values(Vehicle).includes('car')) {
    // Do stuff here
}

Here is what the above code is Doing:
1. Object.values(Vehicle) returns an array of the values of the Vehicle enum.
2. .includes(‘car’) checks if the array contains the value ‘car’.

Similar Posts