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

111
Visualizações
TypeScript - Access sub class type from base class

I'm trying to construct a base class that would be inherited by some sub classes. Inside the base class, I'd like to add multiple static creator functions that'd be easy to use in the codebase. The problem is, I can't access the type of the sub class from the base class.

For example, this is the base class:

class BaseClass {
  static fromText(text: string) {
    const element = getByText(text);
    return new this(element);
  }
  
  static fromRole(role: string) {
    const element = getByRole(role);
    return new this(element);
  }
}

And this is the sub class:

class SubClass extends BaseClass {
  doSomething() {
    console.log('doing something');
  }
}

Now, I want to call doSomething from SubClass. The problem is, TypeScript doesn't know this function exist inside SubClass:

const subclass = SubClass.fromText('hello');
subclass.doSomething(); // TypeScript error here, saying doSomething doesn't exist

Does anyone know how can I achieve this?

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