我收到了警告…

对装饰器的实验性支持是一个在未来版本中可能会更改的特性。设置'experimentalDecorators'选项'以删除此警告。

... 即使我的编译选项在tsconfig。Json有以下设置:

"emitDecoratorMetadata": true,
"experimentalDecorators": true,

奇怪的是,一些使用装饰器的随机类不显示警告,但同一项目中的其他类会显示警告。

在TypeScript编译器中,什么会导致这种行为?


当前回答

在tsconfig中添加以下行。重新启动VS Code。

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

其他回答

打开整个项目的文件夹而不是project-name/src

tsconfig。Json不在SRC文件夹中

为了清晰和愚蠢。

1) 打开 .vscode/settings.json。

2)添加“typescript”。Tsdk ": "node_modules/typescript/lib"

3)保存。

4)重启Visual Studio代码。

如果您打开存在于项目外部的TS文件,您可能会遇到这个问题。例如,我正在使用lerna,并从另一个包中打开了一个文件。尽管另一个包有自己的tsconfig和实验性的装饰器,但VsCode并不尊重它。

如果您正在使用cli编译*。在ts文件中,可以使用以下命令设置experimentalDecorators:

 tsc filename.ts --experimentalDecorators "true"

您可以使用此代码运行

 tsc .\src\index.ts --experimentalDecorators "true" --emitDecoratorMetadata "true"