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

370
Views
¿Por qué funciona mi método reduce () si defino la matriz en TypeScript pero no cuando uso un archivo modelo?

He definido este objeto en el archivo TypeScript y he usado el siguiente método reduce() y no hay ningún problema con él.

 sampleList = { totalRecordsTest: {}, sampleList: [ { parent: 'Sample Text 2', children: [ { id: 1, text: "Sample Text 1" }, ], }, { parent: 'Sample Text 2', children: [ { id: 2, text: "Sample Text 2" }, ], }, { parent: 'Sample Text 3', children: [ { id: 3, text: "Sample Text 3" }, ], }, ] } let grouped = this.sampleList.sampleList.reduce((groups, current) => { groups[current.parent] = groups[current.parent] || []; groups[current.parent].push(...current.children); return groups; }, Object.create(null)); this.groupedSampleList = Object.keys(grouped).map((key) => ({ parent: key, children: grouped[key], }));

Sin embargo, cuando traté de crear un archivo de interfaz de modelo para "sampleList" en otro archivo, aparece el error El tipo 'indefinido' no se puede usar como un tipo de índice en el siguiente método para la variable actual .

Archivo mecanografiado

 let grouped2 = this.sampleList.reduce((groups, current) => { groups[current.parent] = groups[current.parent] || []; groups[current.parent].push(...current.children); return groups; }, Object.create(null));

Importar modelo en TypeScript

 sampleList: SampleList[] = []

Archivo modelo

 export interface SampleListModel { parent?: string | '-', children?: ChildrenSampleModel[]; } export class SampleList implements SampleListModel { constructor( public parent?: string | '-', public children?: ChildrenSampleModel[] ) { } } export interface ChildrenSampleModel { id?: number, text?: string } export class ChildrenSample implements ChildrenSampleModel { constructor( public id?: number, public text?: string ) { } }

Solución

El parent se declara como opcional, por lo que puede no estar definido y cuando lo uso como índice arrojará el error "El tipo 'indefinido' no se puede usar como un tipo de índice en el siguiente método para la variable actual".

 export interface SampleListModel { parent: string, children: ChildrenSampleModel[]; } export class SampleList implements SampleListModel { constructor( public parent: string, public children: ChildrenSampleModel[] ) { } }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede deberse a que parent es opcional en el constructor de SampleList , por lo que puede no estar definido y no puede usarlo como índice.

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!