【问题标题】:Visual Studio not compiling TypeScriptVisual Studio 未编译 TypeScript
【发布时间】:2016-09-12 17:44:15
【问题描述】:

我有一个结构如下的 Visual Studio 项目:

我的tsconfig.json 看起来像:

{
  "compilerOptions": {
    "noImplicitAny": false,
    "noEmitOnError": true,
    "removeComments": false,
    "sourceMap": true,
    "target": "es5",
    "outDir": "../wwwroot/"
  },
  "exclude": [
    "node_modules",
    "wwwroot"
  ]
}

但是,VS 不会将app.ts 编译到wwwroot 文件夹中。

我错过了什么?

【问题讨论】:

  • 您是否在项目属性 -> 构建中选中了“编译时编译 TypeScript”?
  • 这是...我没有建造!我认为它是连续的,而不是在构建中。谢谢指点。
  • @BanksySan 您可能正在“属性”>“TypeScript 构建”中寻找“保存时编译”

标签: visual-studio typescript visual-studio-2015 asp.net-core


【解决方案1】:

尝试将"compileOnSave": true 添加到您的tsconfig.json

{
    "compilerOptions": {
        "noImplicitAny": false,
        "noEmitOnError": true,
        "removeComments": false,
        "sourceMap": true,
        "target": "es5",
        "outDir": "../wwwroot/"
     },
     "exclude": [
        "node_modules",
        "wwwroot"
    ],
    "compileOnSave": true
}

现在每次保存时都应该编译。

【讨论】:

  • 为我做到了。
  • 也为我工作!注意:compilerOptions.noEmit 必须删除或设置为 false 才能使此解决方案起作用。
【解决方案2】:

Visual Studio 中,Project > Properties > Build > 确保“Compile TypeScript on build " 被选中:

此外,在您的 tsconfig.json 中,您应该指定一个 rootDir,以便 TypeScript 知道在哪里寻找应该编译的 *.ts 文件:

{
  "compileOnSave": true,
  "compilerOptions": {
    "noImplicitAny": false,
    "noEmitOnError": true,
    "removeComments": false,
    "sourceMap": true,
    "target": "es5",
    "outDir": "../wwwroot/",
    "rootDir": "../wwwroot/"
  },
  "exclude": [
    "node_modules",
    "wwwroot"
  ]
}

详情在 TypeScript 页面herehere 上标注。

不要忘记实际构建它。这不是文件观察程序。

【讨论】:

  • 干杯大卫。结果发现我的困惑中缺少的一点是我没有意识到它是在构建时触发的,我认为它一直在监视文件。
  • Visual Studio 是否真的使用了 tsconfig.json 中的设置?
  • @VernJensen 绝对是,如果它们被省略,那么它们依赖于默认值。
  • 我的 asp.net 项目中没有这个选项
【解决方案3】:

可能是打字稿文件中的语法错误。

我的解决方案是回滚 .ts 文件直到它编译完成(即 typescript 和 javascript 文件的时间戳相同)并从那里开始更改 .ts 文件。如果编译.ts 文件后时间戳不一致,那么我们知道这可能是语法错误。我必须在每次更改后编译并检查时间戳。

不幸的是,语法错误没有显示在任何地方,所以我不得不依靠时间戳来确定文件是否被编译。

我在 Visual Studio 2019 中启用了保存时编译


以下是 FileExplorer 的屏幕截图,其中.ts 出现错误。注意时间戳的不同。

下面是 FileExplorer 的屏幕截图,其中.ts 文件成功编译为`javascript。 Moice 时间戳是相同的。

【讨论】:

    猜你喜欢
    • 2018-04-14
    • 1970-01-01
    • 2012-09-25
    • 2017-08-28
    • 2020-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多