在TypeScript中动态更改枚举的值 [英] Dynamically change the value of enum in TypeScript

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

问题描述

看看下面的代码:

// typescript enum example
enum foo { ONE = 1, TWO = 2, THREE = 3 }

是否有可能将ONE的值更改为0运行?如果是,怎么办?

Is it possible to change the value of ONE to 0 in runtime? If yes, how?

推荐答案

我认为打字稿编译器不会允许这样做。但是,为什么要更改枚举?重点是要命名常量值。如果您希望它们进行更改,为什么不只使用以下内容:

I don't think the typescript compiler will allow this. But why would you want to change an enum? The whole point is to have named constant values. If you want them to change, why not just use something like this:

const foo = {
  ONE: 1,
  TWO: 2,
  THREE: 3
}

这篇关于在TypeScript中动态更改枚举的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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