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

171
Views
Creating an array from condition

i need to create an array, but the object type is different depending on a condition, something like this:

  myList = condition()
      ? Array < { secondLabel: String, value: String } > []
      : Array < { label: String, value: Number } > []

but, Im getting these errors:

Cannot compare class Array [1] to object literal [2].

Cannot compare boolean [1] to empty array literal [2].
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Just use the array constructor !

const myList = true ?
  new Array<{ secondLabel: String, value: String }>()
  : new Array<{ label: String, value: Number }>()

or you can asert the type :

const myList = condition()
  ? [] as Array<{ secondLabel: String, value: String }>
  : [] as Array<{ label: String, value: Number }>

Playground

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!