简体   繁体   English

有没有办法在打字稿中创建环境中的枚举?

[英]Is there a way to create enums in typescript that are ambient?

edit after release of typescript 0.9: enums are now supported: 在打印typescript 0.9之后编辑: 现在支持枚举:

enum Select { every, first, last }

original question: 原始问题:

Enums in typescript were discussed here but no solution leads to an ambient design. 这里讨论了打字稿中的枚举,但没有解决方案导致环境设计。 An ambient enum definition would mean that the enum is handled by the compiler only and the compiled js output files only deal with the raw numerical values. 环境枚举定义意味着枚举仅由编译器处理,而编译的js输出文件仅处理原始数值。 Like in C++11. 就像在C ++ 11中一样。

The closest I get is 我得到的最接近的是

declare var Color = { red: 1, blue: 2, green: 3 } // DOES NOT COMPILE

but the compiler does not accept this: "Ambient variable cannot have initializer". 但是编译器不接受这个:“Ambient变量不能有初始化器”。

edit 编辑
incorporating dmck's answer: 结合dmck的答案:

declare var Color: { Red: number; Green: number; Blue: number; };

This does not output any js code. 这不会输出任何js代码。 In other words, it is ambient. 换句话说,它是环境。 This however makes it useless as well: 然而,这也使它无用:

declare var Color: { Red: number; Green: number; Blue: number; };

function test() {
  var x = Color.Blue;    // null ref exception, no Color object
  console.log(x == Color.Red);
}

will produce a runtime error, as Color is not defined in js. 将产生运行时错误,因为未在js中定义Color。 The ts declaration just claims that there is some Color object in js, while in fact, without definition, there is none. ts声明只是声称js中有一些Color对象,而事实上,没有定义,没有。 Ti fix this we can add 我们可以添加Ti解决这个问题

var Color = {
   Red: 1,
   Green: 2,
   Blue: 3
};

but now the enum implementation is not "ambient" in the sense that the typescript compiler does what a C++ compiler does, reducing the enum values at all occurencies to plain numbers. 但是现在enum实现不是“环境”,因为typescript编译器执行C ++编译器所做的事情,将所有出现的枚举值减少为普通数字。 The current ambient declarations allow type checking but not such replacement. 当前的环境声明允许类型检查,但不允许这样的替换。

TypeScript 0.9允许这样:

declare module A { export enum E { X, Y, Z } }

Adding an answer as there are new features for this starting with TypeScript 1.4; 添加答案,因为有从TypeScript 1.4开始的新功能;

using const enum give the expected result. 使用const enum给出预期的结果。

This is with a normal enum: 这是一个正常的枚举:

//TypeScript:
enum StandardEnum {FirstItem, SecondItem, ThirdItem};

//Compiled javascript:
var StandardEnum;
(function (StandardEnum) {
    StandardEnum[StandardEnum["FirstItem"] = 0] = "FirstItem";
    StandardEnum[StandardEnum["SecondItem"] = 1] = "SecondItem";
    StandardEnum[StandardEnum["ThirdItem"] = 2] = "ThirdItem";
})(StandardEnum || (StandardEnum = {}));

This is with a const enum: 这是一个const enum:

//TypeScript:
const enum ConstantEnum { FirstItem, SecondItem, ThirdItem };

//Compiled javascript:
;

This blog post explains it with more details : https://tusksoft.com/blog/posts/11/const-enums-in-typescript-1-4-and-how-they-differ-from-standard-enums 这篇博文详细解释了它: https//tusksoft.com/blog/posts/11/const-enums-in-typescript-1-4-and-how-they-differ-from-standard-enums

You could do something like this: 你可以这样做:

declare var Color: { Red: number; Green: number; Blue: number; };

Remember that by design ambient declarations have no effect on the output of your code . 请记住,通过设计, 环境声明对代码的输出没有影响 So somewhere you'll need to actually declare Color and the values of each enum member. 所以某处你需要实际声明Color和每个枚举成员的值。

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

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