打字稿中的常量枚举(tsc.js vs typescript.js) [英] const enum in typescript (tsc.js vs typescript.js)

查看:23
本文介绍了打字稿中的常量枚举(tsc.js vs typescript.js)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在打字稿中有一个 const enum:

I have a const enum in typescript:

const enum LogLevel {
    TRACE = 0,
    DEBUG = 1,
    INFO = 2,
    WARN = 3,
    ERROR = 4,
    SILENT = 5
}

基于打字稿规范以下字段:

Based on the typescript spec the following field:

private foo: number = LogLevel.DEBUG;

应编译为:

this.foo = 1 /* DEBUG */;

当我从命令行 (Windows) 使用 tsc 时,它按预期工作.但是当它在 webpack 项目中使用 awesome-typescript-loader 编译时(它使用 node_modules 中的 typescript.js 而不是 tsc 使用的 tsc.js),那么枚举常量是没有被内联:

When I use tsc from the command line (Windows) it works as expected. But when it is compiled with awesome-typescript-loader in a webpack project (which uses the typescript.js from node_modules as opposed to the tsc.js which is used by tsc), then the enum constant is not getting inlined:

this.foo = LogLevel.DEBUG;

tsc 和 node 模块版本都一样(2.0.2).

Both the tsc and the node module version are the same (2.0.2).

我认为两者之间应该没有区别.有人知道为什么吗?

I think there should not be a difference between the two. Does anybody know why?

推荐答案

原来是由 tsconfig.json 中的 declaration 选项引起的.如果设置为false,则两次编译产生上述不一致的结果.但是当它设置为 true 时,它​​会按预期工作.不知道为什么这个标志对结果有这样的影响.

It turned out it was caused by the declaration option in tsconfig.json. If it is set to false, the two compilations produce the above inconsistent result. But when it is set to true, it works as expected. Not sure why this flag has such an effect on the outcome.

这篇关于打字稿中的常量枚举(tsc.js vs typescript.js)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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