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

657
Vistas
Getting ts 7053 error (Typescript) with my Array of Objects that I created

I've seen other StackOverflow answers, but none of them seem to be working. Here is the error I am getting.

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Paycheck'.
  No index signature with a parameter of type 'string' was found on type 'Paycheck'.

I get this when I try to iterate over the Array, and using their keys to get their value. Is there anything wrong with the way I have created my types?

const seperated: Record<string, Array<string>> = {
    date: [],
    taxes: [],
    benefits: [],
    retirement: [],
    takeHome: [],
    total: [],
  };

type Paycheck = {
  date: string;
  taxes: number;
  benefits: number;
  retirement: number;
  takeHome: number;
  total: number;
};

// removing some code to go straight to forEach loop
// p is of type Paycheck
paychecks.forEach(p => {
    // seperated is pretty much the keys of paycheck
    Object.keys(seperated).forEach(key => {
      // p[key] is throwing the error I mentioned before
      seperated[key].push(p[key]);
    });
  });

What am I doing wrong? How do I add a index signature appropriately? I've looked at other solutions and can't get it to work.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

What you can do is to get the key from Paycheck via keyof.

let k = key as keyof Paycheck;

And since all the properties (value) in seperated is with Array<string> type.

const seperated: Record<string, Array<string>> = {
  date: [],
  taxes: [],
  benefits: [],
  retirement: [],
  takeHome: [],
  total: [],
};

Hence, you need to cast the value from each property in Paycheck as string before the use of push.

seperated[k].push(p[k].toString());

Complete code

paychecks.forEach(p => { 
  Object.keys(seperated).forEach(key => {
    let k = key as keyof Paycheck;
    seperated[key].push(p[k].toString());
  });
});

Sample Demo on Typescript Playground


Unless you define the indexable key signature for the Type, then you can pass the key as string for index accessing for the Type:

type Paycheck = {
  date: string;
  taxes: number;
  benefits: number;
  retirement: number;
  takeHome: number;
  total: number;
  [key: string]: string | number; // Here to specify key as string and union Value type
};
seperated[key].push(p[key].toString());

Sample Demo (Define Indexable key Signature) on Typescript Playground

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