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

Nat*_*end 80 javascript compilation visual-studio typescript visual-studio-2015

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

这是我尝试过的:

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

    <PropertyGroup>
        <TypeScriptCompileOnSaveEnabled>True</TypeScriptCompileOnSaveEnabled>
    </PropertyGroup>
    
    Run Code Online (Sandbox Code Playgroud)
  • 检查并取消选中"自动编译不属于项目的TypeScript文件"选项Tools -> Options -> TypeScript -> Project:

    在此输入图像描述

  • 仔细检查以确保在我的项目的TypeScript Build属性中选中"Compile on save":

    在此输入图像描述

我错过了什么?

作为旁注,TypeScript编译步骤在由常规构建触发时按预期工作.

小智 133

对我来说,这是以下选项tsconfig.json:

"compileOnSave": true,
"compilerOptions": { ... },
Run Code Online (Sandbox Code Playgroud)

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

  • 这对我有用,但只有在重新启动VS之后. (29认同)
  • 是的,在tsconfig.json中添加选项,如果项目托管在VS解决方案中,则重新加载项目.然后ts将在保存时编译为js. (4认同)

Dar*_*eal 32

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

{
  "compilerOptions": {
    "watch": true
  }
}
Run Code Online (Sandbox Code Playgroud)

在这之后,我不得不解决与输出窗口中出现的以下错误相关的另一个问题:

Object doesn't support property or method 'watchFile'
Run Code Online (Sandbox Code Playgroud)

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

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

C:\Program Files (x86)\Microsoft SDKs\TypeScript
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

有关其他详细信息,请在我的博客上阅读此文章.


Nat*_*end 6

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

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


小智 6

解:

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

您需要添加“ 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"
  ]
}
Run Code Online (Sandbox Code Playgroud)

最好的祝福,

Anders Both Basechat。