使用 get set“exited with code 1”编译 TypeScript 错误代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12770447/
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:13:16  来源:igfitidea点击:

Compiling TypeScript error code with get set "exited with code 1"

javascripttypescript

提问by DexDude

get topLeft()      { return this._topLeft;             }

set topLeft(value) {  this._topLeft = value; Recalc(); }

The above code works find in TypeScript Play, but I received build error when compiling it from Visual Studio 2012 error "exited with code 1"

上面的代码可以在 TypeScript Play 中找到,但是从 Visual Studio 2012 编译时我收到了构建错误 error "exited with code 1"

Does anyone try get,set in TypeScript and build successfully?

有没有人尝试在 TypeScript 中获取、设置并成功构建?

回答by ndm

You'll need to target ECMAScript v5, ie pass the -target ES5argument to the compiler. This needs to be set in the project files target configuration.

您需要以 ECMAScript v5 为目标,-target ES5即将参数传递给编译器。这需要在项目文件目标配置中设置。

I don't know if VS has any built in mechanims for editing target configurations, so i can only tell you how to do it manually. Simply open your .csprojproject file, look for the Targetnode where the TypeScript compiler command is located, and add the -target ES5argument.

我不知道VS是否有任何内置的编辑目标配置的机制,所以我只能告诉你如何手动完成。只需打开您的.csproj项目文件,查找TargetTypeScript 编译器命令所在的节点,然后添加-target ES5参数。

In my config it looks like this:

在我的配置中它看起来像这样:

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

Update

更新

As of version 0.8.1.0, the hardcoded version dependency was removed and support for source maps was added, and so the Targetnode now looks like this by default:

从 0.8.1.0 版本开始,删除了硬编码的版本依赖项并添加了对源映射的支持,因此Target默认情况下节点现在看起来像这样:

<Message Text="Executing tsc --target ES5 $(TypeScriptSourceMap) @(TypeScriptCompile ->'&quot;%(fullpath)&quot;', ' ')" />
<Exec Command="tsc --target ES5 $(TypeScriptSourceMap) @(TypeScriptCompile ->'&quot;%(fullpath)&quot;', ' ')" />

Injecting the targetargument is still pretty easy, simply put it after tscor $(TypeScriptSourceMap):

注入target参数仍然很容易,只需将其放在tscor之后$(TypeScriptSourceMap)

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.targets" />

回答by Asher Barak

As of 0.8.2 has had another change. Some of the common TypeScript build stuff was moved from your .csproj to an external build file. Like so:

从 0.8.2 开始又有了另一个变化。一些常见的 TypeScript 构建内容已从 .csproj 移至外部构建文件。像这样:

  <PropertyGroup Condition="'$(Configuration)' == 'Debug'">
    <TypeScriptTarget>ES3</TypeScriptTarget>
    <TypeScriptIncludeComments>true</TypeScriptIncludeComments>
    <TypeScriptSourceMap>true</TypeScriptSourceMap>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)' == 'Release'">
    <TypeScriptTarget>ES3</TypeScriptTarget>
    <TypeScriptIncludeComments>false</TypeScriptIncludeComments>
    <TypeScriptSourceMap>false</TypeScriptSourceMap>
  </PropertyGroup>

Your .csproj still gets to set some arguments on the TypeScript build by including them as elements in the build. one of those element is the ES version. The template created two groups for me, one for debug and one for release:

您的 .csproj 仍然可以通过将它们作为元素包含在构建中来在 TypeScript 构建上设置一些参数。其中一个元素是 ES 版本。该模板为我创建了两组,一组用于调试,一组用于发布:

##代码##

for the desired effect just change the ES3 to ES5

对于所需的效果只需将 ES3 更改为 ES5

For deeper understanding of how this eventually ends up as part of the call to the TypeScript compiler have a look in the Microsoft.TypeScript.targets file.

要更深入地了解这最终如何作为对 TypeScript 编译器的调用的一部分,请查看 Microsoft.TypeScript.targets 文件。

Good luck,

祝你好运,