在VSCode中获取Javascript类型提示而不导入,这将导致循环依赖

问题描述 投票:1回答:1

我有一种情况,类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中获取类型提示,但这会创建循环依赖关系。有没有办法在不导入的情况下获得类型提示?

autocomplete visual-studio-code type-hinting
1个回答
2
投票

在使用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;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.