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

Posted

技术标签:

【中文标题】使用 get set "exited with code 1" 编译 TypeScript 错误代码【英文标题】:Compiling TypeScript error code with get set "exited with code 1" 【发布时间】:2012-09-28 00:54:48 【问题描述】:
get topLeft()       return this._topLeft;             

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

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

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

【问题讨论】:

【参考方案1】:

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

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

在我的配置中是这样的:

<Target Name="BeforeBuild">
    <Exec Command="&quot;$(PROGRAMFILES)\Microsoft SDKs\TypeScript\0.8.0.0\tsc&quot; -target ES5 @(TypeScriptCompile ->'&quot;%(fullpath)&quot;', ' ')" />
</Target>

更新

从 0.8.1.0 版开始,移除了硬编码的版本依赖项并添加了对源映射的支持,因此 Target 节点现在默认如下所示:

<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>

注入target 参数还是很简单的,只需将它放在tsc$(TypeScriptSourceMap) 之后:

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

【讨论】:

不客气 :) 请注意,建议将解决问题的答案标记为已接受(旁边的小复选标记),这不仅关乎声誉,还有助于其他人选择正确的信息(假设有更多答案),这是其他提问者和受访者表示问题已解决的指标,当其他人看到问题不会白费时,他们会更愿意回答您的问题。跨度> 我的 BeforeBuild 目标有一个 Exec Command=tsc$(TypeScriptSourceMap) @... 行,而不是你提到的 "$(PROGRAMFILES)... 行。我是否安装错误?你知道用那种 Exec Command 风格把 ES5 目标放在哪里吗? 我猜您使用的是较新的版本,我的描述与 0.8.0.0 版本有关,即初始预览/alpha 版本。我现在无法对其进行测试,但看起来应该可以:tsc --target ES5 $(TypeScriptSourceMap) ... 我稍后会检查并适当更新我的答案。【参考方案2】:

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

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

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

  <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>

为了获得想要的效果,只需将 ES3 更改为 ES5

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

祝你好运,

【讨论】:

在我将 TypeScriptTarget 编辑为 ES5 后,我收到错误消息:"tsc.exe" exited with code 1. 有什么想法吗?

以上是关于使用 get set "exited with code 1" 编译 TypeScript 错误代码的主要内容,如果未能解决你的问题,请参考以下文章

js Proxy

linux中的set命令: "set -e" 与 "set -o pipefail"

php有效电子邮件

java中get,set的使用

pycharm 运行pygame 没反应

使用$_GET["action"]取值时必须先判断action是否存在。