Home > Mobile >  How can I annotate that argument is a class and has certain static method?
How can I annotate that argument is a class and has certain static method?

Time:09-06

I want to pass a class to function and call its static method. The code works but autocomplete does not. How can I annotate that argument has certain static method?

class Parent {
  static staticMethod() {
    console.log("static method")
  }
}
class Child1 extends Parent {}
class Child2 extends Parent {}

/** @param {Parent} child */
function doSomething(child) {
  child.staticMethod(); // no autocomplete
}
doSomething(Child1)

enter image description here

ADDED

Marked as duplicate and suggested inappropriate question; it is talked about library but I ask about what happens in a single file itself.

Writing jsdoc documentation on methods inside a class

CodePudding user response:

Use typeof:

/**
 * @param {typeof Parent} child
 */
  • Related