Typescript 没有将枚举编译为数字
Typescript not compiling enums to numbers
我正在尝试使用 typescript 本质上使用模式编写 json 数据,因此我需要 Typescript 编译器将枚举编译为 js 输出中的数字,但我无法让它工作。
我已经尝试了很多东西,this post is similar to the issue I'm having but I'm not using awesome-typescript-loader, I've also tried everything 堆栈溢出 post 但没有任何乐趣。我的 ts-config 在下面
{
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": false,
"removeComments": true,
"preserveConstEnums": false,
"sourceMap": false
},
"include": [
"src/**/*"
]
}
我只是在根目录的命令行中调用 tsc
,它编译所有内容,但枚举被编译为 MyEnum_1.MyEnum.FirstValue
。
我尝试过的其他事情;
- 将模块更改为各种不同的选项
- 将目标更改为各种不同的选项
- 添加不同的选项并使用不同的 lib 值。
我正在 运行ning tsc -v 3.6.4 并且 运行 没有想法,有没有人遇到过类似的问题?
编辑:下面是我正在导入的枚举类型的示例
export enum MyEnum {
Undefined,
FirstValue,
SecondValue,
ThirdValue
}
编辑:我的打字稿文件示例是...
import { MyEnum } from "../../src/models/MyEnum";
import { ItemType } from "../../src/models/ItemType";
var item: ItemType = {
"id": "00000000-0000-0000-0000-000000000000",
"value": MyEnum.FirstValue
}
我希望它能编译成...
"use strict";
exports.__esModule = true;
var MyEnum_1 = require("../../src/models/MyEnum");
var ItemType_1 = require("../../src/models/ItemType");
var item = {
"id": "00000000-0000-0000-0000-000000000000",
"value": 1
};
而是编译为;
"use strict";
exports.__esModule = true;
var MyEnum_1 = require("../../src/models/MyEnum");
var ItemType_1 = require("../../src/models/ItemType");
var item = {
"id": "00000000-0000-0000-0000-000000000000",
"value": MyEnum_1.MyEnum.FirstValue
};
我几乎不关心它是如何编译的,只要枚举编译成一个数字即可。
您正在寻找 const enums:
export const enum MyEnum {
Undefined,
FirstValue,
SecondValue,
ThirdValue
}
var item = {
"id": "00000000-0000-0000-0000-000000000000",
"value": MyEnum.FirstValue
}
编译为
var MyEnum;
(function (MyEnum) {
// stuff
})(MyEnum || (MyEnum = {}));
var item = {
"id": "00000000-0000-0000-0000-000000000000",
"value": 1 /* FirstValue */
};
如果您想完全跳过枚举声明,也将其设为 ambient:
export declare const enum MyEnum {
...
}
var item = {
"id": "00000000-0000-0000-0000-000000000000",
"value": MyEnum.FirstValue
}
只给出
var item = {
"id": "00000000-0000-0000-0000-000000000000",
"value": 1 /* FirstValue */
};
我正在尝试使用 typescript 本质上使用模式编写 json 数据,因此我需要 Typescript 编译器将枚举编译为 js 输出中的数字,但我无法让它工作。
我已经尝试了很多东西,this post is similar to the issue I'm having but I'm not using awesome-typescript-loader, I've also tried everything
{
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": false,
"removeComments": true,
"preserveConstEnums": false,
"sourceMap": false
},
"include": [
"src/**/*"
]
}
我只是在根目录的命令行中调用 tsc
,它编译所有内容,但枚举被编译为 MyEnum_1.MyEnum.FirstValue
。
我尝试过的其他事情;
- 将模块更改为各种不同的选项
- 将目标更改为各种不同的选项
- 添加不同的选项并使用不同的 lib 值。
我正在 运行ning tsc -v 3.6.4 并且 运行 没有想法,有没有人遇到过类似的问题?
编辑:下面是我正在导入的枚举类型的示例
export enum MyEnum {
Undefined,
FirstValue,
SecondValue,
ThirdValue
}
编辑:我的打字稿文件示例是...
import { MyEnum } from "../../src/models/MyEnum";
import { ItemType } from "../../src/models/ItemType";
var item: ItemType = {
"id": "00000000-0000-0000-0000-000000000000",
"value": MyEnum.FirstValue
}
我希望它能编译成...
"use strict";
exports.__esModule = true;
var MyEnum_1 = require("../../src/models/MyEnum");
var ItemType_1 = require("../../src/models/ItemType");
var item = {
"id": "00000000-0000-0000-0000-000000000000",
"value": 1
};
而是编译为;
"use strict";
exports.__esModule = true;
var MyEnum_1 = require("../../src/models/MyEnum");
var ItemType_1 = require("../../src/models/ItemType");
var item = {
"id": "00000000-0000-0000-0000-000000000000",
"value": MyEnum_1.MyEnum.FirstValue
};
我几乎不关心它是如何编译的,只要枚举编译成一个数字即可。
您正在寻找 const enums:
export const enum MyEnum {
Undefined,
FirstValue,
SecondValue,
ThirdValue
}
var item = {
"id": "00000000-0000-0000-0000-000000000000",
"value": MyEnum.FirstValue
}
编译为
var MyEnum;
(function (MyEnum) {
// stuff
})(MyEnum || (MyEnum = {}));
var item = {
"id": "00000000-0000-0000-0000-000000000000",
"value": 1 /* FirstValue */
};
如果您想完全跳过枚举声明,也将其设为 ambient:
export declare const enum MyEnum {
...
}
var item = {
"id": "00000000-0000-0000-0000-000000000000",
"value": MyEnum.FirstValue
}
只给出
var item = {
"id": "00000000-0000-0000-0000-000000000000",
"value": 1 /* FirstValue */
};