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

143
Views
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 answers
Answer question

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 Report

0

first check if params[key] is defined

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

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 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!