【问题标题】:Not able to compile .ts to .js in Vs - windows无法在 Vs 中将 .ts 编译为 .js - windows
【发布时间】:2017-10-12 22:35:38
【问题描述】:

我是 typescript 的新手,我想做的就是将我的 typescript 文件转换为 javascript 当我执行 npm typescript --verison 时,我得到 3.10.10

我有这样的 task.json 文件

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "0.1.0",
    "command": "${cwd}/node_modules/.bin/tsc.cmd",
    "isShellCommand": true,
    "args": [
        "-p",
        "."
    ],
    "showOutput": "silent",
    "problemMatcher": "$tsc"
}

和 tsconfig.json 这样的文件

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2016",
      "dom"
    ]
  }
}

当我执行 tsc mock.ts tsc 时,我收到这样的错误

'tsc' 不是内部或外部命令,可操作 程序或批处理文件。

在博客中进行了一些研究后,我在我的 tasks.json 文件中使用了它

"command": "${cwd}/node_modules/.bin/tsc.cmd", 但我无法将我的 .ts 文件编译成 javascript

【问题讨论】:

    标签: javascript node.js typescript typescript-compiler-api


    【解决方案1】:

    希望对您有所帮助的几点说明

    • npm typescript --verison 正在返回 npm 的版本,而不是 TypeScript(3.1 已经非常过时,所以您可能需要先更新 npm i -g npm)
    • 运行 tsc 的方式会有所不同,具体取决于您是全局安装 (npm install typescript) 还是本地安装 (npm install --save-dev typescript)。

    如果您将脚本添加到 package.json 文件,它应该为您处理定位 typescript 包,无论它是全局安装还是本地安装。例如:

    在 package.json 中

    ... "scripts": { "tsc": "tsc" } ...

    然后像这样运行 TypeScript 编译器:npm run tsc -- <your typescript options>。例如:npm run tsc -- --version 然后应该返回 typescript 的实际版本(或者告诉你 TypeScript 没有真正安装)。

    如果您想进一步了解是否安装了 TypeScript,也可以查看 npm ls。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-01
      • 2020-08-28
      • 2016-04-19
      • 2021-08-26
      • 1970-01-01
      • 1970-01-01
      • 2023-02-23
      • 1970-01-01
      相关资源
      最近更新 更多