PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, April 12, 2022

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

 April 12, 2022     es6-modules, intellisense, javascript-intellisense, jsdoc, visual-studio-code     No comments   

Issue

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.



Answered By - Vlad Haidei
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing