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

157
Vistas
Typescript generic interface with field that references the generic class (not instance) type

Let's say I have a typescript class A, and a class B that extends A

class A {
  static a(): number { return 10; }
  a_(): number { return 10; }
}

class B extends A {
  static b(): number { return 20; }
  b_(): number { return 20; }
}

I would like to define a generic typescript interface, like so:

interface ClassAndInstance<C extends A> {
  c: ClassType<C>
  i: C
}

(Note: ClassType here refers to the class (not instance) of the generic type.)

Now, if I have an instance of the ClassAndInstance

ci: ClassAndInstance<B>;

I want to be able to call both the class and the instance fields of B, like so:

ci.c.a();  // 10
ci.c.b();  // 20
ci.i.a_(); // 10
ci.i.b_(); // 20;

How would I define ClassType to achieve this in a type safe manner?

I've chosen an interface to define the semantics behind ClassAndInstance, but if there's any other way of achieving this, that would work too.

about 4 years ago · Juan Pablo Isaza
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