Search code examples
javascriptvisual-studiojsdoccode-completion

JavaScript Intellisense for classes declared in a self-invoking function does not work in Visual Studio


I would like to use Visual Studio's IntelliSense but it doesn't work in this situation. Here is an example of what I am trying to do:

// IntelliSense doesn't work.
Namespace.Class = (function () {
    /** A class. */
    function Class() {
        /** A method. */
        this.method = function () {
            console.log("test 1");
        };
    }
    return Class;
}());

// Intellisense works.
/** A class. */
Namespace2.Class = function () {
    /** A method. */
    this.method = function () {
        console.log("test 2");
    };
};

Examples of what IntelliSense shows

Not working:

Doesn't work.

Working:

Works!

Notes:


Solution

  • Try the following snippet. For some unknown reason, Intellisense infers the result of the function as typeof Class instead of just Class. To fix it typecast the type back to Class again.

    Namespace.Class = (function () {
      /** A class. */
      function Class() {
        /** A method. */
        this.method = function () {
            console.log("test 1");
        };
      }
      return /** @type {Class} */(/** @type {unknown} */(Class));
    }());