Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

144
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

0

first check if params[key] is defined

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

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda