Javascript TypeScript“保存时编译”功能在 Visual Studio 2015 中不起作用

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

TypeScript "Compile on save" feature not working in Visual Studio 2015

javascriptvisual-studiocompilationtypescriptvisual-studio-2015

提问by Nathan Friend

The "Compile on save" feature isn't working for me after upgrading to Visual Studio 2015. When I make a change to a .tsfile in my project and save, the status bar at the bottom of the IDE says Output(s) generated successfully, but the generated .jsfile doesn't change.

升级到 Visual Studio 2015 后,“保存时编译”功能对我不起作用。当我更改.ts项目中的文件并保存时,IDE 底部的状态栏显示Output(s) generated successfully,但生成的.js文件没有不改变。

Here's what I've tried:

这是我尝试过的:

  • adding the following to the root <Project>element in my .csproj:

    <PropertyGroup>
        <TypeScriptCompileOnSaveEnabled>True</TypeScriptCompileOnSaveEnabled>
    </PropertyGroup>
    
  • checking and unchecking the "Automatically compile TypeScript files which are not part of a project" option in Tools -> Options -> TypeScript -> Project:

    enter image description here

  • double checking to make sure "Compile on save" is checked in my project's TypeScript Build properties:

    enter image description here

  • 将以下内容添加到<Project>my 中的根元素.csproj

    <PropertyGroup>
        <TypeScriptCompileOnSaveEnabled>True</TypeScriptCompileOnSaveEnabled>
    </PropertyGroup>
    
  • 选中和取消选中“自动编译不属于项目一部分的 TypeScript 文件”选项Tools -> Options -> TypeScript -> Project

    在此处输入图片说明

  • 仔细检查以确保在我的项目的 TypeScript 构建属性中选中了“保存时编译”:

    在此处输入图片说明

What am I missing?

我错过了什么?

As a side note, the TypeScript compilation step does work as expected when triggered by a regular build.

附带说明一下,TypeScript 编译步骤在由常规构建触发时会按预期工作。

采纳答案by Nathan Friend

This issue seems to have been resolved with the most recent update to the TypeScript Language Servicesextension.

此问题似乎已通过TypeScript Language Services扩展程序的最新更新得到解决。

See this answerfor instructions on how to apply this update.

有关如何应用此更新的说明,请参阅此答案

回答by Mi?osz Wierzbicki

For me it was this option in tsconfig.json:

对我来说,这是这个选项tsconfig.json

"compileOnSave": true,
"compilerOptions": { ... },

Restart Visual Studio for this change to take effect.

重新启动 Visual Studio 以使此更改生效。

回答by Darkseal

I stumbled upon this problem today: I fixed that by using the new "watch":truecompiler option, also available through JSON in most recent TypeScript versions:

我今天偶然发现了这个问题:我通过使用新的"watch":true编译器选项修复了这个问题,在最新的 TypeScript 版本中也可以通过 JSON 获得:

{
  "compilerOptions": {
    "watch": true
  }
}

After doing that, I had to solve another issue related to the following error that appeared in the output window:

这样做之后,我必须解决与输出窗口中出现的以下错误相关的另一个问题:

Object doesn't support property or method 'watchFile'

It turned out that my system was using an outdated version of TypeScript(1.0.x), despite I was sure I had a newer one that came with the Visual Studio 2015 Update 1(1.7). If you run into this problem, you can easily check your tsc version by typing tsc -vfrom a command-prompt.

结果证明我的系统使用的是过时版本的TypeScript(1.0.x),尽管我确信我有一个更新的版本,它是Visual Studio 2015 Update 1(1.7)附带的。如果您遇到此问题,您可以通过tsc -v从命令提示符输入来轻松检查您的 tsc 版本。

If it says 1.0.xor anything < 1.7, it's probably due to the fact that you have some old references in your PATH environment variable. Be sure you have 1.7 or later installed by checking inside your Microsoft SDKs folder, which is the one used by Visual Studio to install the TypeScript packages as they get updated:

如果它说1.0.x或任何< 1.7,那可能是因为您的 PATH 环境变量中有一些旧引用。检查 Microsoft SDKs 文件夹,确保安装了 1.7 或更高版本,Visual Studio 使用该文件夹在更新后安装 TypeScript 包:

C:\Program Files (x86)\Microsoft SDKs\TypeScript

If not, update accordingly. Open CPanel> System> Advanced> Environment Variables, select System Variablesand click on Edit; browse through the list looking for any reference to the TypeScript folder, change one of them to make it point to your TypeScript most recent installed version (1.7or above) and delete any other dupes. See also screenshot below:

如果没有,请相应更新。打开CPanel> System> Advanced> Environment Variables,选择System Variables并点击Edit;浏览列表以查找对 TypeScript 文件夹的任何引用,更改其中一个以使其指向您最近安装的 TypeScript 版本(1.7或更高版本)并删除任何其他欺骗。另请参阅下面的屏幕截图:

enter image description here

在此处输入图片说明

For additional details, read this poston my blog.

有关详细信息,请阅读这篇文章在我的博客。

回答by Anders Both

Solution:

解决方案:

For me, and I am quite sure this is also the case for others, this was due to a mistake in the tsconfig.json.

对我来说,我很确定其他人也是如此,这是由于 tsconfig.json 中的错误造成的。

You need to add "compileOnSave": true. But in the global section not inside compilerOptions.

您需要添加“compileOnSave”:true。但是在全局部分不在 compilerOptions 内。

Wrong:
{
  "compilerOptions": {
    "noImplicitAny": false,
    "noEmitOnError": true,
    "removeComments": false,
    "sourceMap": true,
    "target": "es5",
    "compileOnSave": true

  },
  "exclude": [
    "node_modules",
    "wwwroot"
  ]
}

Correct:
{
  "compilerOptions": {
    "noImplicitAny": false,
    "noEmitOnError": true,
    "removeComments": false,
    "sourceMap": true,
    "target": "es5"

  },
"compileOnSave": true,
  "exclude": [
    "node_modules",
    "wwwroot"
  ]
}

Best regards,

此致,

Anders Both Basechat.

安德斯两个 Basechat。

回答by Christopher Thomas

In my case, I had installed Visual Studio Community 2015 along side VS 2012. I had been using Web Essentials for typescript in 2012 which appeared to conflict with 2015.

就我而言,我在 VS 2012 旁边安装了 Visual Studio Community 2015。我在 2012 年一直在使用 Web Essentials 进行打字稿,这似乎与 2015 年有冲突。

Uninstalling Web Essentials in 2012 fixed the issue for me.

在 2012 年卸载 Web Essentials 为我解决了这个问题。

回答by Yvan

With typescript 2 you have to delete "outDir": from your tsconfig. Fix the bug for me in visual studio.

使用 typescript 2,您必须从 tsconfig.xml 中删除“outDir”:在 Visual Studio 中为我修复错误。

回答by Daryl

The "compileOnSave": true,wasn't working for me. I finally figured out that Visual Studio doesn't honor the "compileOnSave": true,value if it is defined in another .jsonfile that you're extending. It has to be in the root in order for it to work.

"compileOnSave": true,不是为我工作。我终于发现如果 Visual Studio"compileOnSave": true,是在.json您要扩展的另一个文件中定义的,则它不支持该值。它必须在根目录中才能工作。

回答by Dominic Williams

In project properties -> "TypeScript Build", you can also simply just uncheck "Do not emit outputs if any errors are reported." Having it checked seems to deactivate transpiling on save, where there is an error or not.

在项目属性->“TypeScript Build”中,您也可以简单地取消选中“如果报告任何错误,则不发出输出”。检查它似乎会在保存时停用转译,无论是否有错误。

回答by WasiF

locate the file i.e. C:\file.ts in your terminal/cmdand type

找到文件,即 C:\file.tsterminal/cmd并键入

tsc file.ts -w // watches for file changes and converts on save

回答by Yue Yin

Exact same problem here. I am using Visual Studio 2015 update 3 and TypeScript 2.9.2.0. In tools/options/projects and solutions/external web tools, I upgraded $(PATH) to the second position. With all these configurations, compileOnSave: true doesn't work for me. The workaround solution is open a command line, run ng build --watchon the side and let node take care of auto compilation

完全相同的问题在这里。我正在使用 Visual Studio 2015 更新 3 和 TypeScript 2.9.2.0。在工具/选项/项目和解决方案/外部网络工具中,我将 $(PATH) 升级到第二个位置。有了所有这些配置, compileOnSave: true 对我不起作用。解决方法是打开一个命令行,ng build --watch在一边运行,让 node 负责自动编译