【问题标题】:Achieve Intellisense in VS Code for my own JS ES6 modules libraries为我自己的 JS ES6 模块库在 VS Code 中实现 Intellisense
【发布时间】:2019-05-04 17:45:51
【问题描述】:

VS Code Intellisense 确实非常有助于处理内部项目文件,为我们提供自动完成功能,甚至利用编写的 JSDoc。

但是,我正在开发不同的 JS 项目,这些项目依赖于定制的 JS 库(带有 Bitbucket Cloud 存储库的 ES6 模块,通过 NPM 或 Yarn 检索)。

当然,我也想从 Intellisense 中受益于这些库,因为我知道 JSDoc 是可用的。但遗憾的是,我似乎找不到方法。

我试图在 jsconfig.json 中显式包含我的库中的文件,但无济于事:

{
  "compilerOptions": {
    "target": "es2017",
    "moduleResolution": "node",
    "checkJs": true,
    "allowSyntheticDefaultImports": true,
    "noEmit": true,
    "noImplicitAny": false,
    "strictNullChecks": false,
    "alwaysStrict": true,
    "sourceMap": false
  },
  "include": ["src/**/*", "node_modules/my-lib/**/*"]
}

有没有简单的方法告诉 VS Code 考虑我的库文件? 据我了解,实现这一目标的一种方法是为每个库编写和发布打字稿定义文件。但是,我觉得我遗漏了一些东西,而且 VS Code 肯定能够处理我的库 JS 文件,就像它处理内部项目文件一样。

【问题讨论】:

    标签: javascript typescript visual-studio-code es6-modules


    【解决方案1】:

    这个问题现在已经很老了,但我最近遇到了同样的问题。这是我想出的。

    正如您所提到的,一种方法是创建类型声明文件。幸运的是,您可以使用 tsd-jsdoc 包从您的 jsdoc cmets 执行此操作。

    我将以下脚本添加到 package.json 以构建类型文件...

    jsdoc -r src -t node_modules/tsd-jsdoc/dist -d lib
    

    这将创建一个lib/types.d.ts 文件。

    在您的 package.json 中添加一个 types 条目,指向该文件。

    确保它作为 prepublishOnly 脚本的一部分运行,以便在运行 npm publish 时文件是最新的。

    通常,我的 src/index.js 文件只包含几行 export { default as thing } from './thing'; 行。为了获得 vscode 可以读取的正确声明文件,我必须像这样格式化文件......

    /** @module my-module */
    
    export { default as thing1 } from './thing1';
    export { default as thing2 } from './thing2';
    

    然后在thing1和thing2的jsdoc cmets中为export添加@memberof module:my-module,例如...

    /**
     * Thing 1
     * @param {string} thing
     * @memberof module:my-module
    */
    function thing1(thing) {
    }
    
    export default thing1;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-08
      • 1970-01-01
      • 1970-01-01
      • 2020-03-09
      • 2016-11-17
      • 2021-05-17
      • 1970-01-01
      • 2022-07-17
      相关资源
      最近更新 更多