简体   繁体   English

打字稿:字符串枚举返回未定义

[英]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: 从第一个console.log输出中可以看到,另一个枚举的定义如下:

export enum ViewSide {
    LEFT,
    RIGHT
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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