Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

372
Vistas
Why is my reduce() method working if I define the array in the TypeScript but not when I use a model file?

I have defined this object in the TypeScript file and have use the following reduce() method and there is no issue with it.

  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],
    }));

However, when I tried to create a model interface file for the "sampleList" in another file I am getting the error Type 'undefined' cannot be used as an index type in the following method for the current variable.

TypeScript File

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

Import model in TypeScript

sampleList: SampleList[] = []

Model File

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
    ) { }
}

Solution

The parent is declared as optional which is why it can be undefined and when I use it as an index it will throw the error "Type 'undefined' cannot be used as an index type in the following method for the current variable."

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 Respuestas
Responde la pregunta

0

It may be because parent is optional in the constructor of SampleList, so it can be undefined and you can't use it as an index.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda