[英]Typescript. How to get enum item data as a type
我有一个枚举类型,例如Currency
。 我无法更改它,因为它会自动构建在 graphql 架构上。 我想将它用于我的数据,但我不知道该怎么做。
enum Currency {
rub = 'RUB',
usd = 'USD',
eur = 'EUR',
}
const data: { currency: Currency[keyof typeof Currency] } = {
currency: 'RUB',
};
错误
TS2339: Property 'eur' does not exist on type 'Currency'.
TS2339: Property 'rub' does not exist on type 'Currency'.
TS2339: Property 'usd' does not exist on type 'Currency'.
我不确定是否有可能完全按照你想做的去做,但很可能你想做这样的事情:
const data: { currency: Currency } = {
currency: Currency.rub,
};
这就是枚举的典型使用方式(通过在获取其值之一时始终显式引用枚举)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.