Mechazawa
Mechazawa

Reputation: 83

Documenting classes that are generated through functions (mixing)

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

Answers (1)

strdr4605
strdr4605

Reputation: 4362

Update

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);

solution 2


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();

solution 1

But still, need to figure out how to work with Generic Types. Probably this question may help.

Upvotes: 4

Related Questions