1

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)
4
  • Why do you make setList a class field in TS? Commented Mar 9, 2022 at 21:53
  • so i can convert array string to array object of person, and as it is a object it can holds other many property like name and other many methods that can do different work on those property. and this method is there exactly, because it will be easy to remember that the class itself can generate its own list. I hope, I am able to answer your question Commented Mar 10, 2022 at 3:41
  • 1
    I'm not questioning why the method exists but why you make it a class field. You can keep it as a prototype method, just like in the non-TS version setList(list: string[]) { ... }. Otherwise every object gets its own copy of the function which seems unnecessary. Commented Mar 10, 2022 at 6:16
  • thank you for the suggestion. I am new to ES and typescript and did not realize the impact. Commented Mar 10, 2022 at 8:04

1 Answer 1

2

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!

Sign up to request clarification or add additional context in comments.

1 Comment

yes i need to use this.constructor any way because this person class can be extends by other classes. and in this case using ["constructor"]without typeof Person; helped me. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.