我有一种情况,类A
创建B
的实例,并发送自己作为参考之一。即
a.js
import B from './b';
class A {
constructor() {
this.b = new B(this);
}
}
b.js
class B {
/**
* @param {A} aInstance - The instance of A.
*/
constructor(AInstance) {
this.a = AInstance;
}
}
我想在b.js中导入A以在VSCode中获取类型提示,但这会创建循环依赖关系。有没有办法在不导入的情况下获得类型提示?
在使用TypeScript 2.8进行IntelliSense的VS Code 1.22中并不容易。但是,应该在VS Code 1.24中选择的TypeScript 2.9增加了对jsdocs中类型导入的支持:import('path/to/module')
:
a.js
import B from './b';
export class A {
constructor() {
this.b = new B(this);
}
}
b.js
export class B {
/**
* @param {import('./a').A} aInstance - The instance of A.
*/
constructor(AInstance) {
this.a = AInstance;
}
}