Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

96
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!