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

178
Vistas
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 Respuestas
Responde la pregunta

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