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

171
Views
¿Cómo puedo eliminar el tipo 'falso' dentro de un genérico setState usado en reaccionar?

Hola, estoy tratando de usar useState y manejar un estado con genéricos. Dentro de setState, manipulé una matriz obtenida con la función de filtro y, como saben, devuelve falso.

así que... mostraré mis códigos.

 const [mapItemsState, setMapItems] = useState<{ [k: string]: number | string }[]>([]); setMapItems( Object.entries(data)?.map( ([key, value]) => Object.keys(mapItemListKor).includes(key) && { [key]: value as string | number, }, ) .filter(Boolean) // I erased a false here! )

y este código devuelve este error Argument of type '(false | { [x: string]: string | number; })[]' is not assignable to parameter of type 'SetStateAction<{ [k: string]: string | number; }[]>'.

Eliminé false usando el filtro pero aún detecta false .

¿Cómo puedo manejarlo?

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

 const [mapItemsState, setMapItems] = useState<{ [k: string]: number | string }[]>([]); setMapItems( Object.entries(data).filter(Boolean)?.map( ([key, value]) => Object.keys(mapItemListKor).includes(key) && { [key]: value as string | number, }, ) )

El problema que estaba teniendo es que no estaba filtrando la matriz antes de que se mapeara, lo que significa que los falsos aún se pasan al .map

about 4 years ago · Santiago Gelvez Report

0

De todos modos, resolví este problema de la siguiente manera.

 setMapItems( Object.entries(data) .filter(([key]) => Object.keys(mapItemListKor).includes(key), ) .map(([key, value]) => { return { [key]: value as string | number }; }), );
about 4 years ago · Santiago Gelvez 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!