【问题标题】:How to enable experimentalDecorators in Typescript如何在 Typescript 中启用实验装饰器
【发布时间】:2019-08-25 06:10:13
【问题描述】:

这个问题与那些询问如何抑制由代码编辑器(例如 VSCode)发出的类似警告的问题不同。

我的问题是 Tsc 命令行编译器警告:

greet.ts:7:7 - 错误 TS1219:对装饰器的实验性支持是 未来版本中的功能可能会发生变化。设置 'experimentalDecorators' 选项可移除此警告。

这是我的代码:

function doMore(target) {
    target.doMore = true;
}

@doMore
class Test {
    do() {
        console.log('done');
    }
}  


var t = new Test();
t.do();
console.log(t.doMore);

我在根目录下创建了以下tsconfig.json:

{
    "compilerOptions": {
        "experimentalDecorators": true,
        "allowJs": true
    }
}

但是tsc 仍然在抱怨。

【问题讨论】:

  • 在这里工作正常。你确定它选择了正确的 tsconfig 吗?
  • @georg tsconfig.json 与 greet.ts 位于同一个文件夹中 - 这是我唯一的源文件。创建 tsconfig.json 后,我的 VSCode 抑制了相同的警告
  • 如果你像 tsc greet.ts 那样调用它,它不会使用 tsconfig...
  • 请告诉我如何让 tsc 使用 tsconfig.json
  • typescriptlang.org/docs/handbook/tsconfig-json.html 。基本上,在 tsconfig 中将“greet.ts”添加到files

标签: javascript typescript javascript-decorators


【解决方案1】:

当在命令行中指定输入文件时,tsc 编译器会忽略 tsconfig.js:

`tsc greet.ts1 将简单地忽略 tsconfig.json 文件 - 因此文件中指定的编译器选项不会生效。

tsconfig.json 文件应该包含在源文件路径中,并且应该在不指定源文件的情况下调用tsc 编译器,以便在编译中包含 tsconfig.js 文件。

{
    "compilerOptions": {
        "experimentalDecorators": true,
        "target": "ES5"
    },

    "files": [
        "greet.ts"
    ]
}

【讨论】:

    猜你喜欢
    • 2018-11-19
    • 2018-06-08
    • 2017-11-27
    • 2016-11-11
    • 2021-11-01
    • 2021-10-08
    • 2015-09-02
    • 2022-08-15
    • 2019-05-19
    相关资源
    最近更新 更多