0
打字稿中是否可以在枚举中使用字符串变量? 我可以使用字符串枚举这样的:在打字稿的枚举中使用字符串变量
enum AllDirections {
TOP = 'top',
BOTTOM = 'bottom',
LEFT = 'left',
RIGHT = 'right',
}
但这代码:
const top: string = 'top'
const bottom: string = 'bottom'
const left: string = 'left'
const right: string = 'right'
enum AllDirections {
TOP = top,
BOTTOM = bottom,
LEFT = left,
RIGHT = right,
}
结果与错误:Type 'string' is not assignable to type 'AllDirections'
为什么要'顶部*'和*'AllDirections.TOP'? – jonrsharpe
这只是一个错误重现的例子。事实上,我试图从一个文件中导入一个包含所有可用操作的redux动作类型列表,并将它们分配给另一个文件中的枚举,以便能够使用此枚举类型作为reducer中的类型。 – Anton