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

192
Visualizações
How to get store API response of type array in typescript interface?

The api returns a response like this:

[
  {
    "id": "string",
    "code": "string",
    "name": "string",
  }
]

So for example it will be

[
  {
    "id": "abc",
    "code": "CODE1",
    "name": "name1",
  },
  {
    "id": "cdf",
    "code": "CODE2",
    "name": "name2",
  }
]

How do I store it in a typescript interface? I initially tried it like this

export interface promotionCodes {
  id: string,
  code: string,
  name: string,
}

export interface getPromotionsResponse {
  data: promotionCodes[]
}

and I call it as:

return await this.get<getPromotionsResponse>(
  `myurl`,
);

But I don't think this will work since I don't have anything named 'data' being returned in my call but I'm not sure how else to store it? Thanks for any help

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can create a type that is an array of the other type:

export type getPromotionsResponse = promotionCodes[];

If you have to create an interface and not a type, you'll need to do it like this:

export interface getPromotionsResponse {
    [index: number]: promotionCodes;
}

Or better yet, as Mack suggested in the comments, you can also extend the array interface, so your new type would have all the functionalities of an array.

export interface getPromotionsResponse extends Array<promotionCodes> {}
about 4 years ago · Juan Pablo Isaza Relatório

0

You should use typescript generics for this purpose Here is the link to the docs: https://www.typescriptlang.org/docs/handbook/2/generics.html

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