javascript - 在 Visual Studio Code 中使用装饰器时出错

标签 javascript typescript visual-studio-code

我在尝试使用装饰器将 TypeScript 编译为 JavaScript 时遇到了问题。我得到的错误是这样的:

app.ts:11:7 - error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option in your 'tsconfig' or 'jsconfig' to remove this warning.

11 class Person {
         ~~~~~~

我一直在 StackOverflow 中搜索修复它的方法,也在其他网站上搜索,但似乎没有任何方法适合我。这是我已经尝试过的:

  • 我的 tsconfig.json 文件中已经有 "experimentalDecorators": true
  • 我在设置文件中添加了 "javascript.implicitProjectConfig.experimentalDecorators": true
  • 我尝试禁用我所有的 VSCode 扩展
  • 我尝试多次重启 VSCode 并以多种方式打开项目文件夹

无论如何,我总是收到同样的错误。我的 TypeScript 版本是 4.1.2

编辑:我将我的项目上传到谷歌驱动器 this link .

最佳答案

我建议您再做一件事,您可以通过检查您的 tsconfig.json 来查看它是否包含您的源文件。

我假设您的文件位于:src/index.ts,因此您可能还需要将其包含到配置文件中。

tsconfig.json

{
  "compilerOptions": {
    // ...
    "experimentalDecorators": true
  },
  "include": [
    // Include your source file as well
    "src"
  ],
}

如果使用输入文件(您自己的模式)运行

在这种情况下,tsc 不允许您再使用自己的配置,即 tsconfig.json 文件。

因此您需要通过 CLI 指定您的选项,如下所示:

tsc -w '.\src\app.ts' --experimentalDecorators --target es6

关于javascript - 在 Visual Studio Code 中使用装饰器时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65178095/

相关文章:

typescript - 使用类型来安装 es6-shim

angular - 如果 'router-outlet' 是 Angular 组件,则验证它是否是该模块的一部分。 Angular 12

git - VS 代码 : Command to view git remote changes?

windows - 如何在 Windows 下的 VS Code 中运行后台任务?

javascript - 如何垂直对齐 div 中的链接和按钮?

javascript - 从 props 中 react 未定义的 setState

javascript - 找不到Nodeunit命令?

javascript - 如何在所有浏览器中禁用网页的打印屏幕功能?

javascript - 如何检测到在 <input type ="file"/> 选择文件对话框中单击了取消按钮?

azure - 使用 Visual Studio Code 在本地运行和调试 2 Azure 函数