Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

230
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda