Search code examples
javascriptnode.jstypescriptenumsenumeration

Object.values seems not to work in enum type


I have this enum defined in my app:

export enum Status {
    BOOKED = 'B',
    FREE = 'F',
}

and I add this message on the console

console.log ('<------------------------------------>');
        console.log (code.value);
        console.log (Object.values(Status));
        console.log (code.value in Object.values(Status));
        console.log ('<------------------------------------>');

 <------------------------------------>

and I see this on the console, code.value is not included in the enum; I should see true

    B
    [ 'B', 'F' ]
   false

Solution

  • You should use array.include() to check if an array contain a value https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

    const values = Object.values(Status);
    console.log(values.includes(code.value));