Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda