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

653
Views
Obteniendo el error ts 7053 (mecanografiado) con mi matriz de objetos que creé

He visto otras respuestas de StackOverflow, pero ninguna parece funcionar. Aquí está el error que estoy recibiendo.

 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'.

Obtengo esto cuando trato de iterar sobre el Array y uso sus keys para obtener su valor. ¿Hay algún problema con la forma en que he creado mis tipos?

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

¿Qué estoy haciendo mal? ¿Cómo agrego una firma de índice apropiadamente? He buscado otras soluciones y no puedo hacer que funcione.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Lo que puede hacer es obtener la clave de Paycheck a través de keyof .

 let k = key as keyof Paycheck;

Y dado que todas las propiedades (valor) están seperated con el tipo Array<string> .

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

Por lo tanto, debe convertir el valor de cada propiedad en Paycheck como string antes de usar push .

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

Código completo

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

Muestra de demostración en Typescript Playground


A menos que defina la firma de clave indexable para el Tipo , entonces puede pasar la clave como string para acceder al índice para el Tipo:

 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());

Demostración de muestra (Definir firma de clave indexable) en Typescript Playground

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!