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

125
Views
La clase de mecanografiado ES6 crea un objeto de sí mismo dentro

Creé una clase y dentro de esa clase quiero crear una matriz que consista en un objeto en sí mismo. En javascript normal si lo logra de la siguiente manera

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

En mecanografiado

 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; } }

Recibo un error sobre el código this.constructor(lt) de la siguiente manera

 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

En TypeScript, el tipo de this.constructor en una clase siempre es Function ; sin embargo, TypeScript le permite hacer una referencia a una clase dentro de su declaración, por lo que simplemente reemplazar this.constructor con el nombre de la clase, ( Person ), funcionará bien. Vea abajo:

 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; }; }

Si es absolutamente necesario seguir el camino de this.constructor , puede escribir fuertemente el constructor de esta manera:

 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; }; }

¡Espero que esto ayude!

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!