您需要以 ECMAScript v5 为目标,即将-target ES5 参数传递给编译器。这需要在项目文件目标配置中设置。
我不知道VS是否有任何用于编辑目标配置的内置机制,所以我只能告诉你如何手动完成。只需打开 .csproj 项目文件,查找 TypeScript 编译器命令所在的 Target 节点,然后添加 -target ES5 参数。
在我的配置中是这样的:
<Target Name="BeforeBuild">
<Exec Command=""$(PROGRAMFILES)\Microsoft SDKs\TypeScript\0.8.0.0\tsc" -target ES5 @(TypeScriptCompile ->'"%(fullpath)"', ' ')" />
</Target>
更新
从 0.8.1.0 版开始,移除了硬编码的版本依赖项并添加了对源映射的支持,因此 Target 节点现在默认如下所示:
<Target Name="BeforeBuild">
<Message Text="Compiling TypeScript files" />
<Message Text="Executing tsc$(TypeScriptSourceMap) @(TypeScriptCompile ->'"%(fullpath)"', ' ')" />
<Exec Command="tsc$(TypeScriptSourceMap) @(TypeScriptCompile ->'"%(fullpath)"', ' ')" />
</Target>
注入target 参数还是很简单的,只需将它放在tsc 或$(TypeScriptSourceMap) 之后:
<Message Text="Executing tsc --target ES5 $(TypeScriptSourceMap) @(TypeScriptCompile ->'"%(fullpath)"', ' ')" />
<Exec Command="tsc --target ES5 $(TypeScriptSourceMap) @(TypeScriptCompile ->'"%(fullpath)"', ' ')" />