简体   繁体   English

无法在Visual Studio 2012中编译TypeScript文件

[英]Cannot compile TypeScript files in Visual Studio 2012

I downloaded and installed TypeScript extension for VS 2012, I got my first sample compiles by manually dragging the ts file onto the tsc.exe file! 我为VS 2012下载并安装了TypeScript扩展,我通过手动将ts文件拖到tsc.exe文件上来编译我的第一个示例 because no other way to compile the file will work. 因为没有其他方法来编译文件将起作用。

I don't want to manually do this everytime I want to debug a page! 每次我想调试页面时,我都不想手动执行此操作! I have read about 5 articles on the web and none of the solutions work. 我在网上看到了大约5篇文章,但没有一个解决方案有效。 I've tried adding TypeScript compiler to PATH in Windows, I've tried downloading and installing via Nuget Package Manager the Sholo thing, I've tried these pages: 我试过在Windows中添加TypeScript编译器到PATH,我尝试通过Nuget Package Manager下载并安装Sholo的东西,我试过这些页面:

  1. Using TypeScript in cshtml files 在cshtml文件中使用TypeScript
  2. Visual Studio TypeScript Options Visual Studio TypeScript选项
  3. Debugging TypeScript code with Visual Studio 使用Visual Studio调试TypeScript代码
  4. Cannot compile typescript using tsc node module 无法使用tsc节点模块编译打字稿
  5. TypeScript - Visual Studio - d3 TypeScript - Visual Studio - d3
  6. Visual Studio 2012 TypeScript project ignoring two files during compile Visual Studio 2012 TypeScript项目在编译期间忽略两个文件
  7. TypeScript / Visual Studio 2012 / Compilation parameters TypeScript / Visual Studio 2012 /编译参数
  8. Using TypeScript in an existing Visual Studio Web Site project 在现有Visual Studio网站项目中使用TypeScript
  9. Visual Studio TypeScript Options Visual Studio TypeScript选项
  10. Compile generated files in Visual Studio 在Visual Studio中编译生成的文件

And not a single solution has worked. 并没有一个解决方案有效。

I have even downloaded and installed the Web Essentials plugin for Visual Studio and gone to Options menu, then selected "Compile all TypeScript files on Build = True" and "Compile TypeScript on Save = True" - but nothing works. 我甚至下载并安装了Visual Studio的Web Essentials插件,然后进入选项菜单,然后选择“在Build = True上编译所有TypeScript文件”和“在Save = True上编译TypeScript” - 但没有任何作用。

I've read the entire TypeScript website, the specification, and even their discussion tab on the codeplex site. 我已经阅读了整个TypeScript网站,规范,甚至是codeplex网站上的讨论标签。

How can I get TypeScript to compile when I save changes to the .ts file or when I press F5/Debug/Build? 当我将更改保存到.ts文件按F5 / Debug / Build时,如何才能编译TypeScript?

*I don't really want any third-party scripts/plugins, etc unless it's absolutely necessary. *我真的不想要任何第三方脚本/插件等,除非绝对必要。 I've seen a couple of them and it's just ... there's no point. 我见过他们中的几个,这只是......没有意义。 I might as well just write pure JavaScript. 我不妨写一些纯JavaScript。

You have to ensure that the BuildAction must be TypeScriptCompile for your .ts files. 您必须确保BuildAction必须是.ts文件的TypeScriptCompile。 I've added these lines to end of the (.csproj) project file. 我已将这些行添加到(.csproj)项目文件的末尾。 (Before closing Project tag) And It seems working. (在关闭Project标签之前)它看起来很有效。

 <PropertyGroup Condition="'$(Configuration)' == 'Debug'">
<TypeScriptTarget>ES3</TypeScriptTarget>
<TypeScriptIncludeComments>true</TypeScriptIncludeComments>
<TypeScriptSourceMap>true</TypeScriptSourceMap>
<TypeScriptModuleKind>AMD</TypeScriptModuleKind>
 </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<TypeScriptTarget>ES3</TypeScriptTarget>
<TypeScriptIncludeComments>false</TypeScriptIncludeComments>
<TypeScriptSourceMap>false</TypeScriptSourceMap>
<TypeScriptModuleKind>AMD</TypeScriptModuleKind>
</PropertyGroup>
 <Import Project="$(VSToolsPath)\TypeScript\Microsoft.TypeScript.targets" />

I had a similar problem when I renamed my .js files to .ts. 当我将.js文件重命名为.ts时,我遇到了类似的问题。 You have to right click and add a new TypeScript File to your project in order for your .csproj to recognize that the TypeScript compiler should be used on this .ts file. 您必须右键单击并向项目中添加新的TypeScript文件,以便.csproj能够识别应在此.ts文件上使用TypeScript编译器。

You will know it is correct when you see the arrow to expand the .ts file. 当您看到箭头展开.ts文件时,您会知道它是正确的。

在此输入图像描述

If you change a .js file into a .ts file, you can go to the properties panel in Visual Studio and change the Build Action to TypeScriptCompile. 如果将.js文件更改为.ts文件,则可以转到Visual Studio中的“属性”面板,并将“构建操作”更改为TypeScriptCompile。 This is what is missing when you change a file. 这是更改文件时缺少的内容。 This can save you from deleting and re-adding files. 这可以帮助您避免删除和重新添加文件。

I had the same problem. 我有同样的问题。 I couldn't add new .ts file to the project. 我无法将新的.ts文件添加到项目中。 After renaming from .js to .ts file was not compiled. 从.js重命名为.ts后,文件未编译。 It even was not compiled using tsc in command line. 它甚至没有在命令行中使用tsc编译。

BUT! 但! After recreating the file in windows explorer it was compiled successfully. 在Windows资源管理器中重新创建文件后,它已成功编译。 And after manually creating file in windows explorer (not Visual Studio) in Scripts folder and including it to the project - all WORKS. 并在Scripts文件夹中的Windows资源管理器(不是Visual Studio)中手动创建文件并将其包含到项目中 - 所有工作。 I couldn't detect changes between these two files. 我无法检测到这两个文件之间的变化。 But if you create text file in explorer, rename it to .ts and after that include in project and set file's Build Action to TypeScriptCompile - it works. 但是,如果您在资源管理器中创建文本文件,请将其重命名为.ts,然后将其包含在项目中并将文件的构建操作设置为TypeScriptCompile - 它可以正常工作。

plug 插头

I create a NuGet package that adds a afterbuild target to your project so all the included ts files are compiled to js. 我创建了一个NuGet包,它将一个afterbuild目标添加到你的项目中,所以所有包含的ts文件都被编译为js。

Install-Package TypeScript.Compile Install-Package TypeScript.Compile

I had the same problem. 我有同样的问题。 I updated web essentials extention and problem is solved. 我更新了Web要点扩展,问题解决了。 If you have web essentials extention, you can try that. 如果您有web essentials扩展,您可以尝试。

Please check if you have invalid TypeScript. 请检查您是否有无效的TypeScript。 There are some TypeScript errors that don't show up in the Error List, but they prevent TypeScript Compile-On-Save and show a Output generation failed message in the Visual Studio status bar. 有一些TypeScript错误未显示在错误列表中,但它们阻止TypeScript Compile-On-Save并在Visual Studio状态栏中显示Output generation failed消息。

These are the TypeScript errors that can cause this: 这些是可能导致此问题的TypeScript错误:

  • export ing a class which is not inside of a module (v 0.9.5). export一个不在模块内部的类(v 0.9.5)。

Please let me know if there are more such errors, I'll add them here. 如果有更多此类错误,请告诉我,我会在这里添加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM