0

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

TheMaster
  • 45,448
  • 6
  • 62
  • 85
Takuya HARA
  • 499
  • 1
  • 3
  • 17
  • Why pass a `Class` around as an argument when you can reference its static methods directly? – TheAddonDepot Sep 05 '22 at 12:10
  • @TheAddonDepot Maybe there are multiple `Child` classes, each with it's own `staticMethod` acting differently – TheMaster Sep 05 '22 at 13:07
  • @TheMaster If polymorphism is the goal, as in your example, wouldn't it make more sense to use an instance method instead of a static one? Polymorphic static methods just don't make sense to me from an OOP standpoint. It's not typically a feature in OOP languages (but Javascript, being the chimera that it is, somehow allows for it). – TheAddonDepot Sep 05 '22 at 13:28

1 Answers1

2

Use typeof:

/**
 * @param {typeof Parent} child
 */
TheMaster
  • 45,448
  • 6
  • 62
  • 85