>问题
我使用Visual Studio Code,并使用IntelliSense和JsDoc注释。 我有两个带有类声明的模块:
/**
* This is Foo class
*/
export default class Foo {
constructor() {}
}
和
/**
* This is Bar class
*/
export default class Bar {
constructor() {}
/**
* Init Bar from Foo
* @param {Foo} foo instance of Foo
* @returns {Bar}
*/
static initFromFoo(foo) {
return new Bar();
}
}
类Bar
使用Foo
作为方法的参数initFromFoo
但 IntelliSense 不明白@param Foo
被引用class Foo
并且无法正常工作,并说foo:any
,
https://imgbbb.com/images/2019/09/24/image517c0ec9d1027ac9.png
如何使智能感知正常工作?
我试过什么
- 将
./foo
导入./bar
- 这使得 IntelliSense 工作得很好,但我不需要这个导入,我只需要引用类型定义。 - 添加对另一个文件的引用,如在 TypeScript
/// <reference path="./foo">
中 - 没有影响 - 使用下一个内容创建
jsconfig.json
文件:
{
"compilerOptions": {
"target": "es6",
"allowSyntheticDefaultImports": true,
"checkJs": true
},
"exclude": [
"node_modules"
]
}
也没有影响。它只是突出显示错误Cannot find name 'Foo'.ts(2304)
附言它看起来像与 ES6 模块相关的智能感知限制。因为如果我从两个文件中删除export/import
智能感知按预期工作
https://i.ibb.co/CPL1gJC/image.png
https://i.ibb.co/xmXqzSk/image.png
附言很抱歉链接到图像,我的回购太低,无法发布图像。
可以通过import(path(语句导入类型。 例如:
/**
* Init Bar from Foo
* @param {import('./foo').default} foo instance of Foo
* @returns {Bar}
*/
static initFromFoo(foo) {
return new Bar();
}
或
/**
* @typedef {import('./foo').default} Foo
*
* Init Bar from Foo
* @param {Foo} foo instance of Foo
* @returns {Bar}
*/
static initFromFoo(foo) {
return new Bar();
}
附言它仅适用于Visual Studio Code。它不是有效的 JsDoc。