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

151
Views
El tipo 'desconocido[]' no se puede asignar al tipo

Aunque vi algunas otras publicaciones con personas que tenían el mismo problema, no encontré ningún otro caso similar al mío.

Tengo este bloque de código donde necesito dividir un objeto en 4 partes:

 const eventsLastAddedLine1: EventsListType["events"] = eventsLastadded && Object.entries(eventsLastadded?.data) .slice(0, 4) .map((entry) => entry[1]);

Ese código funciona bien, pero Typescript advierte esto (sobre eventsLastAddedLine1 ) en VS Code:

 Type 'unknown[]' is not assignable to type '{ id: number; attributes: { slug: string; name: string; startDate: Date; endDate: Date; }; }[]'.

Si dejo el mouse sobre eventsLastAddedLine1 (para ver su definición de tipo), se ve correcto:

 const eventsLastAddedLine1: { id: number; attributes: { slug: string; name: string; startDate: Date; endDate: Date; }; }[]

Mi definición para EventsListType es la siguiente:

 export type EventsListType = { events: { id: number; attributes: { slug: string; name: string; startDate: Date; endDate: Date } }[]; isReady: boolean; };

No soy un experto en mecanografiado (todavía estoy aprendiendo), así que no puedo encontrar dónde está el problema. ¿Cómo puedo arreglarlo? ¡Gracias!

EDITAR:

De aquí es de donde proviene eventsLastadded :

 const { data: eventsLastadded } = useSWR(`/events?${queryLastAdded}`, swrFetcher);

Los data anteriores tienen el siguiente tipo:

 SWRResponse<any, any>.data?: any
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Según la definición de tipo para useSWR hook , puede escribir la respuesta a EventsListType["events"][] pasándola como un parámetro de tipo genérico como este:

 const { data: eventsLastadded } = useSWR<EventsListType["events"][]>(`/events?${queryLastAdded}`, swrFetcher); // eventsLastadded is typecasted as EventsListType["events"][] | undefined
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!