Typescript: string enum return undefined

Viewed 1770

I have an enum defined like this :

export enum ViewSide {
    Left = 'left',
    Right = 'right'
}

But when I try to use it, it doesn't work as intended:

console.log(ViewSide); // return {0: "LEFT", 1: "RIGHT", LEFT: 0, RIGHT: 1}
console.log(ViewSide.Right); // return undefined instead of 'right'
console.log(ViewSide['Right']); // return undefined

I have used similar enums, but they works correctly and return the string.

Any idea ?

EDIT: Turns out it was just a cache problem. I had defined the enum without the string before, and it stayed like this for a while.

1 Answers

The only explanation is that you have two enums with the same name and have imported the wrong one.

As you can see from the first console.log output, the other enum is defined like this:

export enum ViewSide {
    LEFT,
    RIGHT
}
Related