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)
setLista class field in TS?setList(list: string[]) { ... }. Otherwise every object gets its own copy of the function which seems unnecessary.