Reputation: 83
I'm trying to document the following code but I seem to be unable to make JSDoc document that class or even reference it's existence.
// SomeMixin.js
export default superclass => class SomeMixin extends superclass {
// class code goes here
func() {}
};
import SomeMixin from './SomeMixin';
import { EventEmitter } from 'events';
/**
* @param {SomeMixin} bar
*/
function foo(bar) {
}
class MyClass extends SomeMixin(EventEmitter) {}
const item = new MyClass();
foo(bar);
How can I document this code so that JSDoc sees that SomeMixin
can be inherited (is a class) and that when I make an instance of MyClass
that it inherits the method func
.
Upvotes: 7
Views: 393
Reputation: 4362
With the help of https://stackoverflow.com/a/52335792/5458200 I got:
// SomeMixin.js
/**
* @template T
* @typedef {new(...args: any[]) => T} Constructor
**/
/**
* @template {Constructor<{}>} T
* @param {T} superclass
*/
function mixin(superclass) {
return class SomeMixin extends superclass {
// class code goes here
func() {}
};
}
export default mixin
// index.js
import createMixin from "./SomeMixin";
import { EventEmitter } from "events";
/**
*
* @param {MyClass} bar
*/
function foo(bar) {
bar;
}
class MyClass extends createMixin(EventEmitter) {}
const item = new MyClass();
foo(item);
Here is a version that I tried. Not the best solution but in VSCode I can see func
and methods from EventEmitter
.
// SomeMixin.js
import { EventEmitter } from "events";
/**
* @param {EventEmitter} superclass
*/
function mixin(superclass) {
/**
* @extends EventEmitter
*/
class SomeMixin extends superclass {
// class code goes here
func() {}
}
return SomeMixin;
}
export default mixin;
// index.js
import SomeMixin from "./SomeMixin";
import { EventEmitter } from "events";
class MyClass extends SomeMixin(EventEmitter) {}
const item = new MyClass();
But still, need to figure out how to work with Generic Types. Probably this question may help.
Upvotes: 4