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

115
Views
How to make the static member type of a class point to the type of the subclass
class SingletonBaseClass {
  static singleton: SingletonBaseClass | null = null;

  static instance() {
    if (!this.singleton) {
      this.singleton = new this();
    }
    return this.singleton;
  }

  static destroy() {
    this.singleton = null;
  }
}

class Demo extends SingletonBaseClass {
  hello() { }
}

Demo.instance().hello();

Above is the code, Demo.instance() will be considered an instance of SingletonBaseClass instead of Demo, so Demo.instance().hello() will report a TypeScript error.

Is there any way to make the return type of Demo.instance() be SingletonBaseClass?

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

you could just cast it:

(Demo.instance() as Demo).hello();
about 4 years ago · Santiago Gelvez Report
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!