在 Visual Studio 中使用 TypeScript 以 ES5 为目标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12811034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:14:35  来源:igfitidea点击:

Targeting ES5 with TypeScript in Visual Studio

typescript

提问by Ezward

I would like to use get/set syntax in TypeScript within Visual Studio Express for Web. How do I enable this. I currently get this error when compiling;

我想在 Visual Studio Express for Web 中的 TypeScript 中使用 get/set 语法。我如何启用此功能。我目前在编译时收到此错误;

Property accessors are only available when targeting ES5 or greater

属性访问器仅在面向 ES5 或更高版本时可用

The file being compiled has a build action of TypeScriptCompile. I don't know how to add a the necessary compiler switch from within Visual Studio.

正在编译的文件的构建操作为TypeScriptCompile. 我不知道如何从 Visual Studio 中添加必要的编译器开关。

Any help would be appreciated.

任何帮助,将不胜感激。

采纳答案by mohamed hegazy

You need to pass the -target ES5 to the compiler. The compilation is triggered using an msbuild task in your project file. Your project file probably has a "TypeScriptCompile" target like the onr bellow, just make sure to the target argument is passed. Here is an example:

您需要将 -target ES5 传递给编译器。使用项目文件中的 msbuild 任务触发编译。您的项目文件可能有一个“TypeScriptCompile”目标,就像下面的 onr 一样,只需确保传递了目标参数。下面是一个例子:

<Target Name="TypeScriptCompile" BeforeTargets="Build">
   <Message Text="Compiling TypeScript files" />
   <Exec Command="&quot;$(PROGRAMFILES)\Microsoft SDKs\TypeScript
<TypeScriptTarget>ES3</TypeScriptTarget>
.8.0.0\tsc&quot; -target ES5 @(TypeScriptCompile ->'&quot;%(fullpath)&quot;', ' ')" /> </Target>

回答by Ian Boyd

This has changed with TypeScript 0.8.2. You now change TypeScriptTargetin the .csprojfile from:

这在 TypeScript 0.8.2 中有所改变。您现在TypeScriptTarget.csproj文件中更改:

<TypeScriptTarget>ES5</TypeScriptTarget>

to

  <PropertyGroup Condition="'$(Configuration)' == 'Debug'">
    <TypeScriptTarget>ES5</TypeScriptTarget>
    <TypeScriptIncludeComments>true</TypeScriptIncludeComments>
    <TypeScriptSourceMap>true</TypeScriptSourceMap>
    <TypeScriptModuleKind>AMD</TypeScriptModuleKind>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)' == 'Release'">
    <TypeScriptTarget>ES5</TypeScriptTarget>
    <TypeScriptIncludeComments>false</TypeScriptIncludeComments>
    <TypeScriptSourceMap>false</TypeScriptSourceMap>
    <TypeScriptModuleKind>AMD</TypeScriptModuleKind>
  </PropertyGroup>

MyApp.csproj:

MyApp.csproj

 <Exec Command="&quot;$(PROGRAMFILES)\Microsoft SDKs\TypeScript
<Exec Command="&quot;$(PROGRAMFILES)\Microsoft SDKs\TypeScript##代码##.8.0.0\tsc&quot; --target ES5 @(TypeScriptCompile ->'&quot;%(fullpath)&quot;', ' ')" />
.8.0.0\tsc&quot; @(TypeScriptCompile ->'&quot;%(fullpath)&quot;', ' ')" />

See also Asher Barak answer

另见Asher Barak 的回答

回答by Devon Holcombe

I am using Visual Studio 2013 Update 4 with Web Essentials. Microsoft has made changing the targetted ECMAScript version much easier.

我将 Visual Studio 2013 Update 4 与 Web Essentials 一起使用。Microsoft 使更改目标 ECMAScript 版本变得更加容易。

You can now do the following:

您现在可以执行以下操作:

  1. Right click your project name and click Properties.
  2. In the Properties window select "Typescript Build"
  3. Set ECMAScript version to "ECMAScript 5".
  1. 右键单击您的项目名称,然后单击属性。
  2. 在“属性”窗口中选择“Typescript Build”
  3. 将 ECMAScript 版本设置为“ECMAScript 5”。

I believe ECMAScript 5 is currently the default. You can at present also choose ECMAScript 3 or ECMAScript 6 as targets.

我相信 ECMAScript 5 目前是默认的。您目前还可以选择 ECMAScript 3 或 ECMAScript 6 作为目标。

回答by Gamer

The switch for instructing the TSC.EXE to generate ES5 compatible code is --target ES5 (note the double dashes).

用于指示 TSC.EXE 生成 ES5 兼容代码的开关是 --target ES5(注意双破折号)。

Each project has a file called [Something].csproj (C# project in our case). Open that file using notepad and look for Targetxml element. Change the exec command by adding the --target ES5.

每个项目都有一个名为 [Something].csproj 的文件(在我们的例子中是 C# 项目)。使用记事本打开该文件并查找Targetxml 元素。通过添加 .exec 命令来更改 exec 命令--target ES5

Before:

前:

##代码##

After:

后:

##代码##

回答by user1110435

Using Studio 2012, project template type TypeScript the build, in the project csproj file is set to ES3

使用Studio 2012,项目模板类型TypeScript构建,在项目csproj文件中设置为ES3

ES3

ES3

Change it to ES3 to ES5, save it and reload the project.

将其更改为 ES3 到 ES5,保存并重新加载项目。