Typescript 中的常量枚举 [英] const enum in Typescript

查看:48
本文介绍了Typescript 中的常量枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 Typescript 的 React 应用程序.现在我遇到了 const 枚举的问题.这是我的枚举:

export const enum Snack {苹果 = 0,香蕉 = 1,橙色 = 2,其他 = 3}

我尝试匹配的服务不​​返回值,而是返回枚举中项目的索引.因此,例如,如果用户设置为吃苹果零食,则该服务将为该用户返回 0 而不是Apple".理想情况下,我想做类似的事情:

varsnackIndex = UserSnack.type;//在这个例子中返回 0var userSnack = Snack[snackIndex];//将返回苹果"

当我尝试类似的操作时,出现以下错误:

错误 TS2476:只能使用字符串文字访问 const 枚举成员.

由于我从中接收数据的服务不返回字符串,因此我在使其正常工作时遇到了问题.

感谢任何帮助.

解决方案

只需移除 const 修饰符即可.

枚举中的

const 表示枚举在编译期间被完全擦除.Const 枚举成员在使用站点内联.您不能通过任意值对其进行索引.换句话说,下面的 TypeScript 代码

const enum Snack {苹果 = 0,香蕉 = 1,橙色 = 2,其他 = 3}让零食= [零食苹果,零食香蕉,零食.橙色,零食.其他];

被编译为:

let Snacks = [0/* 苹果 */,1/* 香蕉 */,2/* 橙色 */,3/* 其他 */];


与非常量版本比较:

enum Snack {苹果 = 0,香蕉 = 1,橙色 = 2,其他 = 3}让零食= [零食苹果,零食香蕉,零食.橙色,零食.其他];

它被编译为:

var Snack;(功能(小吃){小吃[小吃[苹果"] = 0] =苹果";小吃[小吃[香蕉"] = 1] =香蕉";小吃[小吃[橙色"] = 2] =橙色";小吃[小吃[其他"] = 3] =其他";})(零食||(零食={}));让零食= [零食苹果,零食香蕉,零食.橙色,零食.其他];

来源:const enums @ typescriptlang.org

I have a React application that is using Typescript. Right now I'm running into an issue with const enum. Here's my enum:

export const enum Snack {
    Apple = 0,
    Banana = 1,
    Orange = 2,
    Other = 3
}

The service I'm trying to match up to isn't returning the value, but the index of the item within the enum. So, for instance, if the user is set to snack on an apple, the service is returning a 0 for that user instead of 'Apple'. Ideally, I'd like to do something like:

var snackIndex = UserSnack.type; // returning 0 in this example
var userSnack = Snack[snackIndex]; // would return 'Apple'

When I try something similar I'm getting the following error:

error TS2476: A const enum member can only be accessed using a string literal.

Since the service I'm receiving the data from doesn't return the string, I'm having issues getting this working.

Any help is appreciated.

解决方案

Just remove the const modifier.

const in an enum means the enum is fully erased during compilation. Const enum members are inlined at use sites. You can can't index it by an arbitrary value. In other words, the following TypeScript code

const enum Snack {
  Apple = 0,
  Banana = 1,
  Orange = 2,
  Other = 3
}

let snacks = [
  Snack.Apple,
  Snack.Banana,
  Snack.Orange,
  Snack.Other
];

is compiled to:

let Snacks = [
    0 /* Apple */,
    1 /* Banana */,
    2 /* Orange */,
    3 /* Other */
];


Compare it with non-const version:

enum Snack {
  Apple = 0,
  Banana = 1,
  Orange = 2,
  Other = 3
}

let Snacks = [
  Snack.Apple,
  Snack.Banana,
  Snack.Orange,
  Snack.Other
];

it is compiled to:

var Snack;
(function (Snack) {
    Snack[Snack["Apple"] = 0] = "Apple";
    Snack[Snack["Banana"] = 1] = "Banana";
    Snack[Snack["Orange"] = 2] = "Orange";
    Snack[Snack["Other"] = 3] = "Other";
})(Snack || (Snack = {}));
let Snacks = [
    Snack.Apple,
    Snack.Banana,
    Snack.Orange,
    Snack.Other
];

Source: const enums @ typescriptlang.org

这篇关于Typescript 中的常量枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆