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

98
Visualizações
How to declare an object of arrays of custom type in typescript

I am searching how to declare a variable where I can store users by birthdate while avoiding using any

let formatedUsers = {} as "TYPE_NEEDED"
  for (let i = 0; i < users.length; i++) {
    const user= users[i];
    if (!formatedUsers[user.birthdate]) formatedUsers[user.birthdate] = [];
    formatedUsers[user.birthdate].push(user);
  }

In the end I want my variable "formatedUsers" to be like this:

formatedUsers = {
12-02-1996: [user1, user3, user4],
02-04-1998: [user2],
05-08-1999: [user5, user6]
}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

The object keys are strings, and the object values are arrays of users, so a relatively simple Record will do it. Assuming you have a reference to the type of user, you can do:

const formattedUsers: Record<string, User[]> = [];

If you don't have a reference to the user type, you can extract it first.

type User = (typeof users)[number];
about 4 years ago · Juan Pablo Isaza Relatório

0

In that case, you can use the Record interface.

let formatedUsers: Record<number, User[]> = {0:[]};

You can also initialize the value like that.

about 4 years ago · Juan Pablo Isaza Relatório

0

This is more accurate since it requires keys to be number-number-number

const formattedUsers: Record<`${number}-${number}-${number}`, User[]> = {};
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