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

107
Views
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
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!