Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

236
Views
JSDoc: how to hint this.constructor.staticSomething?

I am calling a static method from the constructor of an anonymous javascript class using this.constructor.coords(), but the returned type is not being recognized correctly. It is treated as any.

I know I can cast just cast it manually as {x: number; y: number;}[], but I wonder if there is a smarter way to make VSCode/JSDoc just notice that the type of this.constructor.something must be that of the corresponding static method or property. Alternatively, I wonder if it is possible in JSDoc to cast this.cls = this.constructor with the anonymous class type (without giving it a name), because that would solve the problem for this.constructor.something as well.

MyAnonymousClass = class {

  static coords(){
    return [{x:0, y:1}, {x:1, y:0}, {x:-1, y:-1}];
  }
  /* type of coords (on mouse hover in vsCode):
    (method) MyAnonymousClass.coords(): {
      x: number;
      y: number;
    }[] */ // (CORRECT)

  constructor(){
    const x = this.constructor.coords();
    // type of x (on mouse hover in vsCode): any // (INCORRECT)

  }
}
AnotherAnonymousClass = class extends MyAnonymousClass{
  static coords(){
    return [{x:0, y:2}, {x:-2, y:0}];
  }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The following should do the trick. I've just wrapped the class declaration around an IIFE so it's never accessible outside. It's like an anonymous class.

const MyAnonymousClass = (() => {
    return class _ {
        static coords() {
            return [{ x: 0, y: 1 }, { x: 1, y: 0 }, { x: -1, y: -1 }];
        }

        constructor() {
            const x = /** @type {typeof _} */(this.constructor).coords();
        }

        foo() { }
    }
})();

const AnotherAnonymousClass = class extends MyAnonymousClass {
    static coords() {
        return [{ x: 0, y: 2 }, { x: -2, y: 0 }];
    }
}

Screenshot

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!