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

136
Views
El tipo 'indefinido' no se puede asignar al tipo 'cadena' al destruir un objeto

Estoy destruyendo un objeto de la siguiente manera:

 const { lang, ...params } = ctx.query;

Pero TS me da un error en esta línea:

 if (!value.includes(params[key])) return false;

ingrese la descripción de la imagen aquí

¿Cómo puedo solucionar este problema?

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

0

Use .includes(params[key] as string) (o .includes(<string>params[key]) ) para decirle a Typescript que sabe que es una cadena.

about 4 years ago · Juan Pablo Isaza Report

0

primero verifique si params[key] está definido

 if (params && params[key]) {...
about 4 years ago · Juan Pablo Isaza Report

0

Typescript está entendiendo que su propiedad representada por params[key] tiene un tipo de array o una string , la propiedad incluso no está undefined porque tal vez key no existe en su objeto.

Primero podría verificar si la propiedad existe. Luego, podría verificar si el tipo de su propiedad es una string esta manera.

 if (params[key] && typeof params[key] === string && !value.includes(params[key])) return false;

Lo ideal sería que tuvieras un padre if bloquearas así.

 if(params[key] && typeof params[key] === string){ //The property exists and if of type string, do your thing with it. if (!value.includes(params[key])) return false; }else{ //Why it's not the type you expected? //Do something, log or throw an error for example }

De esta manera, Typescript sabe que solo llegará al niño if la propiedad params[key] es realmente una cadena, y luego el verificador/compilador de TypeScript no se quejará de esa incertidumbre.

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!