I have a Javascript project containing two files. One file, FooModule.js
, is an IIFE module containing several class definitions (among other irrelevant things), one of which is a parent class to all the others. These classes have full JSDoc documentation on the inside and in the return
object that exposes them. Here's the basic structure:
var FooModule = (function() {
class ParentClass { /* ... */ }
class ChildA extends ParentClass { /* ... */ }
class ChildB extends ParentClass { /* ... */ }
class ChildC extends ParentClass { /* ... */ }
return {
ParentClass: ParentClass,
ChildA: ChildA
ChildB: ChildB
ChildC: ChildC
};
})();
In another file, BarModule.js
, I have another module that defines a variable, let's call it baz
, that will store a reference to one of the above child classes. Hoping to take advantage of polymorphism to have documentation tooltips in my IDE (VSCode), I attempt to assign the ParentClass
type to baz
with a JSDoc comment:
/**
* @requires FooModule.js
*/
var BarModule = (function() {
/** @type {ParentClass} */
var baz;
if (condition1)
baz = new FooModule.ChildA();
else if (condition2)
baz = new FooModule.ChildB();
else
baz = new FooModule.ChildC();
})();
My problem is that no matter what I seem to try, VSCode keeps interpreting baz
as type any
instead of correctly producing a reference to ParentClass
. I've tried adding the following to the top of BarModule.js
to no avail:
/**
* @typedef { import("./FooModule.js").ParentClass } ParentClass
*/
I should point out that this did in fact work previously in a slightly different configuration, so I doubt this is a problem strictly with VSCode. In that previously working state, I had all of the contents of FooModule
outside of any IIFE, so everything was on the global scope. I would like to keep them in the IIFE, though, to keep them cleanly namespaced and to keep these classes and their helper methods from polluting the global scope.
What is the proper JSDoc syntax needed to properly tag baz
the way I want without modifying my module structure? Is this kind of thing supported?