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 考虑我的库文件? 据我所知,实现这一目标的一种方法是为每个库编写和发布 typescript 定义文件。但是,我觉得我遗漏了一些东西,而且 VS Code 肯定能够处理我的库 JS 文件,就像它处理内部项目文件一样。
最佳答案
这个问题现在已经很老了,但我最近遇到了同样的问题。这是我想出的。
如您所述,一种方法是创建类型声明文件。幸运的是,您可以使用 tsd-jsdoc 从您的 jsdoc 注释中执行此操作包。
我将以下脚本添加到 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 注释中添加 @memberof module:my-module
,例如 ...
/**
* Thing 1
* @param {string} thing
* @memberof module:my-module
*/
function thing1(thing) {
}
export default thing1;
关于javascript - 在 VS Code 中为我自己的 JS ES6 模块库实现 Intellisense,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53593636/