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

150
Vistas
Type 'undefined' is not assignable to type 'string' when destructing an object

I'm destructing an object as following:

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

But TS gives me an error in this line:

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

enter image description here

How can I solve this issue?

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

0

Use .includes(params[key] as string) (or .includes(<string>params[key])) to tell Typescript you know it's a string.

about 4 years ago · Juan Pablo Isaza Denunciar

0

first check if params[key] is defined

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

0

Typescript is understanding your property represented by params[key] has either a type of an array, or a string, the property is even undefined because maybe key does not exist in your object.

You could first check if the property exists Then you could check if the type of your property is a string this way

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

Ideally you would have a parent if block like this

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
}

This way Typescript knows it will only reach the child if when the params[key] property is really a string, and then the typescript checker/compiler will not complain about that uncertainty.

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