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

119
Views
ES6 typescript class create object of itself inside

I have created a class and inside that class i want to create an array that consist of object of itself. In normal javascript if achieve it as follow

class Person{
     constructor(name){
         this.name=name;
      }
     setList(list){
        let listItem=[];
        for(const lt of list){
           listItem.push(new this.constructor(lt));
        }
       return listItem;
     }
}

In typescript

class Person{
     name:string;
     constructor(name){
         this.name=name;
      }
     setList=(list:Array<string>)=>{
        let listItem=[];
        for(const lt of list){
           listItem.push(new this.constructor(lt));
        }
       return listItem;
     }
}

i get error above code this.constructor(lt) as follow

This expression is not constructable.
  Type 'Function' has no construct signatures.ts(2351)
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In TypeScript, the type of this.constructor in a class is always Function; however, TypeScript allows you to make a reference to a class within its declaration, so, simply replacing the this.constructor with the name of the class, (Person), itself will work fine. See below:

class Person {
  name: string;
  constructor(name: string) {
    this.name = name;
  }
  setList = (list: Array<string>) => {
    let listItem = [];
    for (const lt of list) {
      listItem.push(new Person(lt));
    }
    return listItem;
  };
}

If you absolutely need to go the this.constructor way, you can strongly type the constructor like so:

class Person {
  name: string;
  ["constructor"]: typeof Person;

  constructor(name: string) {
    this.name = name;
  }
  setList = (list: Array<string>) => {
    let listItem = [];
    for (const lt of list) {
      listItem.push(new this.constructor(lt));
    }
    return listItem;
  };
}

Hope this helps!

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