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

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

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