【问题标题】:intellisense in Gruntfile.jsGruntfile.js 中的智能感知
【发布时间】:2016-02-28 00:17:33
【问题描述】:

是否可以让智能感知在 Gruntfile 中工作?

由于“grunt”不是全局变量而是 Gruntfile 中的一个参数,VSCode 将假定它只是一个未知的函数参数“any”。

module.exports = function(grunt) {...}

当我将类型添加到参数时,intellisense 工作正常,但 grunt 不会,因为它是 JS 而不是 TS。

module.exports = function(grunt: IGrunt) {...}

【问题讨论】:

    标签: gruntjs typescript intellisense visual-studio-code


    【解决方案1】:

    如果你想坚持使用 JavaScript,它的运行效果和你的 JavaScript 一样好,但是智能感知和 grunt: IGrunt 一样好:

    /**
     * @param {IGrunt} grunt
     */
    module.exports = function (grunt) {...}
    

    (为什么是的,有 已经有another answer 建议这样做,但我想明确表示这可能是你需要做的全部。)

    在 Visual Studio Code 中,如果您将 typescript.disableAutomaticTypeAcquisition 设置为 true(或该功能遇到某种障碍)并且您的 package.json 中没有 @types/grunt,则此方法将不起作用,但如果任何一种情况都是这样,grunt: IGrunt 似乎也不起作用。

    【讨论】:

      【解决方案2】:

      初学者的步骤...

      设置类型

      为 Grunt 安装 Typings

      npm install --save @types/grunt 
      

      找到打字文件

      确保您的 IDE 知道该类型文件的位置:

      ./node_modules/@types/grunt/index.d.ts
      

      使用自动完成

      这在 .ts 或 .js 中是可能的,无论你喜欢哪个...(我使用 typescript,因为编译器会进行大量类型安全检查)

      Typescript 中的自动完成

      将现有的 gruntfile.js 重命名为 gruntfile.ts。

      添加 IGrunt 类型以启用自动补全:

      module.exports = function (grunt: IGrunt) {
          // auto-complete works for grunt-dot now
          ...
      }
      

      最后,记得编译你的 gruntfile.ts 以便它生成一个新的 gruntfile.js。

      Javascript 中的自动完成

      如果您更愿意避免使用打字稿......

      使用jsdoc在javascript中启用自动完成:

      /**
      * 
      * @param {IGrunt} grunt
      */
      module.exports = function (grunt) {
          // auto-complete works for grunt-dot now
          ...
      }
      

      【讨论】:

        【解决方案3】:

        您可以创建 gruntfile.ts 并将 TypeScript 与智能感知一起使用。然后 GruntJs 将使用编译后的版本 (gruntfile.js)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-02-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多