Gruntfile.js 中的智能感知

Jen*_*ens 2 intellisense gruntjs typescript visual-studio-code

是否可以在 Gruntfile 中使用智能感知?

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

module.exports = function(grunt) {...}
Run Code Online (Sandbox Code Playgroud)

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

module.exports = function(grunt: IGrunt) {...}
Run Code Online (Sandbox Code Playgroud)

dea*_*nde 5

初学者的步骤...

设置类型

为 Grunt 安装 Typings

npm install --save @types/grunt 
Run Code Online (Sandbox Code Playgroud)

找到打字文件

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

./node_modules/@types/grunt/index.d.ts
Run Code Online (Sandbox Code Playgroud)

使用自动完成

这在 .ts 或 .js 中是可能的,无论你喜欢哪个......(我使用打字稿,因为编译器做了很多类型安全检查)

打字稿中的自动完成

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

添加 IGrunt 类型以启用自动完成:

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

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

Javascript 中的自动完成

如果您更喜欢避免打字稿....

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

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