Visual Studio 2015 RC Typescript 实验性装饰器错误

35

如何在VS2015中禁用以下错误提示?

TS1219 实验性的装饰器支持是一个可能会在未来版本中发生变化的功能。请指定“--experimentalDecorators”以移除此警告。

enter image description here

我正在尝试使用Angular2注解。我尝试手动将下面的内容添加到项目文件,但没有成功:

<TypeScriptExperimentalDecorators>True</TypeScriptExperimentalDecorators>

我也安装了TypeScript 1.5.0 beta扩展。


你可以从这个VS 2015快速入门开始。 - jmvtrinidad
11个回答

41

Visual Studio 2015支持从Typescript版本1.8开始使用tsconfig文件。在项目的根目录下创建tsconfig.json文件,并添加experimentalDecorators编译器选项。

示例:

{
  "compileOnSave": true,
  "compilerOptions": {
    "module": "commonjs",
    "sourceMap": true,

    "experimentalDecorators": true
  }
}

对于旧版本:

https://github.com/Microsoft/TypeScript/issues/3934

如果您有项目文件,则 tsconfig 将不受尊重。项目文件优先。我们仍在完善 VS 项目和 tsconfig 之间的集成方式,因为您可以在两者中指定编译器选项。

因此,要禁用错误消息,请右键单击项目文件、取消加载项目,然后右键单击编辑项目文件,在文件末尾应该有一个配置部分,请添加:

<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
  ....
  <TypeScriptExperimentalDecorators>true</TypeScriptExperimentalDecorators>
</PropertyGroup>

保存并重新加载。

您可能还需要TypeScriptEmitDecoratorMetadata来映射--emitDecroatorMetadata。


4
没能为我解决问题,即使在使用2015社区版时,我也试过重新启动IDE。 - wayofthefuture
2
这对我有用。但是给定的文本缺少闭合括号(>)。 - kennethc
@kennethc,现在已经添加了闭括号。 - Greg Gum
2
这对我很有效,使用的是VS 2015社区版更新3。谢谢 :) - Sampath

28
请升级您的IDE到最新版本。如果您还没有,请在项目根目录下创建tsconfig.json文件,然后将experimentalDecorators作为编译选项添加进去。

示例:

{
  "version": "1.5.0",
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "declaration": false,
    "noImplicitAny": false,
    "removeComments": true,
    "noLib": false,
    "emitDecoratorMetadata": true,
    "sourceMap": true,
    "listFiles": true,
    "outDir": "dist",
    "experimentalDecorators": true
  }
}

1
我在VS Code中也遇到了同样的问题。我尝试添加experimentalDecorators标志,但现在它给我返回“error TS5023:未知编译器选项'experimentalDecorators'”。我该怎么办? - Andrei Tătar
8
更新 tsconfig.json 文件后,不要忘记重启 IDE。至少在我使用 VS Code 时是这样的。 - Barry Tormey
6
这是针对Visual Studio Code的答案;问题具体针对的是Visual Studio 2015(可能带有Node Tools for Visual Studio),这是一个完全不同的产品,目前不支持tsconfig.json。 - arolson101

15
在 Visual Studio 2015 中,您需要在 TypeScript 项目文件中启用 TypeScriptExperimentalDecorators
卸载项目文件,然后添加以下行:
<TypeScriptExperimentalDecorators>True</TypeScriptExperimentalDecorators>

我复制了完整的PropertyGroup以便您可以找到添加位置:

<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
  <TypeScriptRemoveComments>false</TypeScriptRemoveComments>
  <TypeScriptSourceMap>true</TypeScriptSourceMap>
  <TypeScriptTarget>ES5</TypeScriptTarget>
  <TypeScriptJSXEmit>None</TypeScriptJSXEmit>
  <TypeScriptCompileOnSaveEnabled>True</TypeScriptCompileOnSaveEnabled>
  <TypeScriptNoImplicitAny>False</TypeScriptNoImplicitAny>
  <TypeScriptModuleKind>System</TypeScriptModuleKind>
  <TypeScriptOutFile />
  <TypeScriptOutDir />
  <TypeScriptGeneratesDeclarations>False</TypeScriptGeneratesDeclarations>
  <TypeScriptNoEmitOnError>True</TypeScriptNoEmitOnError>
  <TypeScriptMapRoot />
  <TypeScriptSourceRoot />
  <TypeScriptExperimentalDecorators>True</TypeScriptExperimentalDecorators>
</PropertyGroup>

同样适用于VS 2017。不确定为什么Visual Studio会忽略tsconfig.json文件,而该设置仍然存在。 - Jacques
卸载和重新加载项目可以解决它。 - CincyBella

4

我发现我还需要添加元数据标签

<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
    <!--other -->
    <TypeScriptExperimentalDecorators>True</TypeScriptExperimentalDecorators>
    <TypeScriptEmitDecoratorMetadata>True</TypeScriptEmitDecoratorMetadata>
</PropertyGroup>

它们似乎需要按照那个顺序排列。 - Ron Newcomb
在我从Visual Studio 2015更新到Visual Studio 2017之后,这个解决了我的问题。 - Alastair

1
我也遇到了这个问题。
在以下情况下遇到:
- 开始使用 Angular2 - 2.0.0-beta.17
我的环境是:
- Visual Studio 2015 Update 3 - Typescript for Microsoft Visual Studio Extension 1.8.34.0 - Node 版本 v6.2.2(输入“>node --version”查找你的版本) - ASP.NET Core 项目
我的解决方案是:
- 卸载项目 - 将以下行添加到项目配置文件中
<TypeScriptExperimentalDecorators>True</TypeScriptExperimentalDecorators>

如果在csproj文件中找不到正确的属性组,或者希望VS环境自动添加正确的属性组,则只需在项目属性>生成下切换“生成时产生输出”,然后卸载项目即可。保存文件并重新加载项目,关闭Visual Studio,然后启动并重新打开解决方案和项目。
需要注意的是,此时我没有tsconfig.json文件。添加tsconfig.json文件会导致问题再次出现。为了进一步解决问题,在添加tsconfig.json文件后,我按照.NET Core上设置typescript (位于此处) 的说明进行操作,包括“为依赖项安装类型”和“更新tsconfig.json”。
总结一下(但请自行阅读):
  • 打开cmd并导航到项目的src文件夹
  • 运行"npm install -g typings"(我不喜欢这个选项,其他选项也可以)
  • 在tsconfig.json的compilerOptions中添加"experimentalDecorators": true
  • 再次检查你的解决方案。上述问题现在应该已经消失了。

为了更好地理解,除了以上错误,我还遇到了以下问题:

上述步骤解决了我的特定错误。希望对你有所帮助。


1

仅使用Nuget软件包管理器,我就能将我的TypeScript升级到最新版本,并解决了这个问题。

据我所见,这与TypeScript版本冲突有关。

请确认您的TypeScript版本,并进行适当的升级或降级以解决此问题。


1

0

这个错误出现是因为我误将一个新文件的扩展名命名为“.js”而不是“.ts”。 将文件扩展名更改为“.ts”后问题解决了。


0
我需要在 VSCode 的 settings.json 文件中添加以下内容以消除警告:
"javascript.implicitProjectConfig.experimentalDecorators": true 操作路径:VSCode -> 首选项 -> 设置

enter image description here


0

我有类似的问题。我有一些发现要分享。我也在使用 Angular 2(ng-book-2)。

我在使用什么?

IDE:WebStorm

TypeScript:1.6.2

时间:2015年10月10日

我在做什么?

我尝试将我的 app.ts 编译为 app.js;但是,我遇到了这个错误:

error TS5052: Option 'emitDecoratorMetadata' cannot be specified without specifying option 'experimentalDecorators'.

这是我的 tsconfig.json AFTER

{
    "version": "1.5.0",
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "declaration": false,
        "noImplicitAny": false,
        "removeComments": true,
        "noLib": false,
        "emitDecoratorMetadata": true
    },
    "filesGlob": [
        "./**/*.ts",
        "!./node_modules/**/*.ts"
    ],
    "files": [
        "./app.ts",
        "./typings/angular2/angular2.d.ts",
        "./typings/es6-promise/es6-promise.d.ts",
        "./typings/rx/rx-lite.d.ts",
        "./typings/rx/rx.d.ts",
        "./typings/tsd.d.ts"
    ]
}

这是我的 tsconfig.json AFTER

{
    "version": "1.6.2",
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "declaration": false,
        "noImplicitAny": false,
        "removeComments": true,
        "noLib": false,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true
    },
    "filesGlob": [
        "./**/*.ts",
        "!./node_modules/**/*.ts"
    ],
    "files": [
        "./app.ts",
        "./typings/angular2/angular2.d.ts"
    ]
}

将tsconfig.json从之前更改为之后有效地解决了我的两个问题。一个是上面的问题,另一个是下面的问题。后一个问题在这里讨论 angular2 with ES6 should not reference es6-promise

typings/es6-promise/es6-promise.d.ts(11,15): error TS2300: 重复标识符'Promise'。 typings/es6-promise/es6-promise.d.ts(42,16): error TS2300: 重复标识符'Promise'。 typings/es6-shim/es6-shim.d.ts(475,11): error TS2300: 重复标识符'Promise'。 typings/es6-shim/es6-shim.d.ts(552,13): error TS2300: 重复标识符'Promise'。


5
很不幸,对于我自己和原帖发布者来说,问题是 Visual Studio 似乎无论将 tsconfig.json 文件放在哪里都不会使用它。我需要翻译的内容已完成。 - Vern Jensen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接