javascript - Typescript -w(watch) 在我的安装中不起作用

标签 javascript node.js typescript typescript2.0

我已经安装了 Typescript 2.0.6,我也在安装 Node 。如果我尝试用“-w”在监视模式下编译一个文件,它是行不通的。它会引发以下错误。

ts5001: the current host does not support the '--watch' option

使用以下命令编译我的 typescript 文件“index.ts”

tsc -w index.ts

如果知道请帮我看看如何在 watch 模式下编译代码。

最佳答案

2021 年的更新答案

有一个内置的--watch/-w 选项见the TS compiler options (在列表的最后)。

或者一旦您使用 outDirrootDir 设置了 tsconfig.json:

{
  "compilerOptions": {
    /* Visit https://aka.ms/tsconfig.json to read more about this file */

    /* Basic Options */
    // "incremental": true,  /* Enable incremental compilation */
    "target": "ES2020",     /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
    "module": "ES2020",   /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
    "outDir": "src/main/resources/static/js",  /* Redirect output structure to the directory. */
    "rootDir": "src/main/resources/static/ts" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
  }
}

然后你就可以打电话了

tsc --watch

第二个选项我还没有亲自尝试过,但它是另一种用于文件监视的选项,请参阅 Configuring Watch有关详细信息,但这里有来自官方网站的示例 tsconfig.json 设置:

{   // Some typical compiler options   
  "compilerOptions": {
    "target": "es2020",
    "moduleResolution": "node"
    // ...   },

  // NEW: Options for file/directory watching   "watchOptions": {
    // Use native file system events for files and directories
    "watchFile": "useFsEvents",
    "watchDirectory": "useFsEvents",

    // Poll files for updates more frequently
    // when they're updated a lot.
    "fallbackPolling": "dynamicPriority"   
  } 
}

关于javascript - Typescript -w(watch) 在我的安装中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40650803/

相关文章:

javascript - 在 TinyMCE 中设置默认字体大小失败

javascript - 如何在页面刷新后保持按钮处于事件状态?

javascript - 重置按钮等效 Javascript

node.js - socket.io 跨源​​请求被阻止

javascript - 在 VS Code 中调试 Node JS 应用程序时未命中断点

javascript - Angular 2 中数组索引越界

javascript - 如何使用 html 页面上所有文本区域的所有名称(或 ID)创建数组?

javascript - 使用 Horseman 和 PhantomJS 下载 wav 文件会丢失数据质量

typescript - 如何配置 create-react-app typescript 以自动将 ts-loader 用于基于 typescript 的节点模块?

typescript - 为给定类型键入合并函数的集合