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

235
Views
How to type custom object?

I am just learning TypeScript and have a question. I am receiving data from the API and would like to type it.

interface FieldData {
  type: number,
  other: string
}

const AAA = 'aaa';
const BBB = 'bbb';
const CCC = 'ccc';


const fields: Array<FieldData> = await getFields();

const myData: any = {
  [AAA]: [],
  [BBB]: [],
  [CCC]: []
};

fields.forEach((field: any) => {
  if (field.type === 1) {
    myData[AAA].push(field);
  } else if (field.type === 2) {
    myData[BBB].push(field);
  } else {
    myData[CCC].push(field);
  }
});

How do I create a typing for a myData object? I don't want to use any type.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If the AAA etc properties may vary, and can't be completely static, write them with as const, and note that their array values are of Array<FieldData>.

const myData = {
  [AAA]: [] as Array<FieldData>,
  [BBB]: [] as Array<FieldData>,
  [CCC]: [] as Array<FieldData>
};

fields.forEach((field) => {

or

const myData: Record<typeof AAA | typeof BBB | typeof CCC, Array<FieldData>> = {
  [AAA]: [],
  [BBB]: [],
  [CCC]: []
};
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!