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

151
Visualizações
Is there a way to have a map with complex keys in typescript?

So I want to implement a map with two-part keys in typescript. My key format is like this:

type Key = {
  section: number,
  index: number
}

I have tried to create a map like this:

let map = new Map<Key,Record>()

But whenever I try to get a value from the map:

cont val = map.get(myKey)

The value is undefined.

Is there any way to have a map in typescript with complex types?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Because you are using an object as a key of Map you need to make sure the reference key that you have used to set the map, is the same when you want to get from the map!

let myKey:Key = {index:1,section:2}
map.set(myKey,your object)
const val = map.get(myKey); // Now has value

But I suggest you to use primitive type as a key. So you can append section and index as new string key!

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