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

265
Vistas
Js immutable - how to flatten a map to a Set<number>

I have a value shipments that is of a type Map<number, Shipment>, Shipment has a property packageIds which is of a type Set<number>. When I have multiple shipments I would like to go through each shipment, get their packageIds and make a flat new Set with all of the packageIds from each shipment:

shipments.flatMap(shipment => shipment.get('packageIds'))

But, if I do that I get a typescript error:

TS2322: Type 'Set<number>' is not assignable to type 'Iterable<[unknown, unknown]>'.
  The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types. 
  Type 'IteratorResult<number, any>' is not assignable to type 'IteratorResult<[unknown, unknown], any>'.       
  Type 'IteratorYieldResult<number>' is not assignable to type 'IteratorResult<[unknown, unknown], any>'.         
  Type 'IteratorYieldResult<number>' is not assignable to type 'IteratorYieldResult<[unknown, unknown]>'.           
  Type 'number' is not assignable to type '[unknown, unknown]'. 

How can I then get a flat Set when iterating over a map?

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

0

The immutablejs flatMap method of Maps returns another Map. That's not what you want. Convert to a Set before, discarding the Map keys:

shipments.toSet().flatMap(shipment => shipment.get('packageIds'))
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