visual-studio-codeintellisensees6-modulesjsdocjavascript-intellisense

How to refer in JsDoc (VS Code / IntelliSense) to class from another file without direct import?


Problem

I use Visual Studio Code and use IntelliSense with JsDoc comments. I have two modules with class declarations:

/**
 * This is Foo class 
 */
export default class Foo {
    constructor() {}
}

and

/**
 * 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();
    }
}

Class Bar use Foo as param for the method initFromFoo but IntelliSense doesn't understand that @param Foo is referred to class Foo and don't work properly and says that foo:any,

https://imgbbb.com/images/2019/09/24/image517c0ec9d1027ac9.png

How can I make IntelliSense work well?

What I have tried

  1. Import ./foo into ./bar- this makes IntelliSense work well but I don't need this import I need just reference to the type definition.
  2. Add reference to another file like in TypeScript /// <reference path="./foo"> - no effects
  3. Create jsconfig.json file with the next content:
{
    "compilerOptions": {
        "target": "es6",
        "allowSyntheticDefaultImports": true,
        "checkJs": true
    },
    "exclude": [
        "node_modules"
    ]
}

no effects too. It just highlights error Cannot find name 'Foo'.ts(2304)

P.S. It looks like IntelliSense limitation that related to ES6 modules. Because if I remove export/import from both files IntelliSense work as expected

https://i.ibb.co/CPL1gJC/image.png

https://i.ibb.co/xmXqzSk/image.png

P.S.S. Sorry for links to images my repo is too low for image posting.


Solution

  • It is possible to import type by import(path) statement. For example:

    /**
     * Init Bar from Foo
     * @param {import('./foo').default} foo instance of Foo
     * @returns {Bar}
     */
    static initFromFoo(foo) {
        return new Bar();
    }
    

    OR

    /**
     * @typedef {import('./foo').default} Foo
     *
     * Init Bar from Foo
     * @param {Foo} foo instance of Foo
     * @returns {Bar}
     */
    static initFromFoo(foo) {
        return new Bar();
    }
    

    P.S. It is hack only for Visual Studio Code. It isn't valid JsDoc.