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

112
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 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