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

111
Views
Crating a type of array of multiple objects

I want to create a type for an array of objects. The array of objects can look like this:

   const troll = [
      {
        a: 'something',
        b: 'something else'
      },
      {
        a: 'something',
        b: 'something else'
      }
    ];

the type i am trying to use is:

export type trollType = [{ [key: string]: string }];

Then i want to use the type like this:

   const troll: trollType = [
      {
        a: 'something',
        b: 'something else'
      },
      {
        a: 'something',
        b: 'something else'
      }
    ];

but i get this error:

Type '[{ a: string; b: string; }, { a: string; b: string; }]' is not assignable to type 'trollType'.
  Source has 2 element(s) but target allows only 1

I can do something like this:

export type trollType = [{ [key: string]: string }, { [key: string]: string }];

but lets say my array of object will have 100 objects in the array.

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

When setting a type for an array it should in this format any[].

So in your case

export type trollType = { [key: string]: string }[];
about 4 years ago · Santiago Gelvez Report

0

You can try to use the Record type for storing the object attribute definitions and create an array out of it, like in the following:

type TrollType = Record<string, string>[];

const troll: TrollType = [
      {
        a: 'something',
        b: 'something else'
      },
      {
        a: 'something',
        b: 'something else'
      }
    ];
about 4 years ago · Santiago Gelvez 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!