如何在Typescript

时间:2019-06-01 06:26:28

标签: typescript

我不明白我在全局定义枚举时缺少什么。

constants.ts

enum CREEP_ROLE {
  HARVESTER = 0,
  BUILDER = 1,
}

main.ts

import './constants'

const test = CREEP_ROLE.HARVESTER;

打字稿编译器将以下代码编译为以下内容:

var CREEP_ROLE$1;
(function (CREEP_ROLE) {
    CREEP_ROLE[CREEP_ROLE["HARVESTER"] = 0] = "HARVESTER";
    CREEP_ROLE[CREEP_ROLE["BUILDER"] = 1] = "BUILDER";
})(CREEP_ROLE$1 || (CREEP_ROLE$1 = {}));
//# sourceMappingURL=constants.js.map

const test = CREEP_ROLE.HARVESTER;

运行生成的文件时,我得到ReferenceError: CREEP_ROLE is not defined。我想念的是什么?

我的tslint:

{
  "compilerOptions": {
    "module": "esnext",
    "lib": ["esnext"],
    "target": "es2017",
    "moduleResolution": "Node",
    "outDir": "dist",
    "baseUrl": "src/",
    "sourceMap": true,
    "strict": true,
    "experimentalDecorators": true,
    "noImplicitReturns": true,
    "allowSyntheticDefaultImports": true,
    "allowUnreachableCode": false
  },
  "exclude": [
    "node_modules"
  ]
}

1 个答案:

答案 0 :(得分:2)

导出您的枚举:

export enum CREEP_ROLE {
    HARVESTER = 0,
    BUILDER = 1,
}

然后像这样导入:

import {CREEP_ROLE} from './constants';