简体   繁体   中英

Typescript: string enum return undefined

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.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM