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
Generic object property

I have this Interface

export interface ChartDataResponseI {
  consumption: string
  generation: string
  measure_name: string
  point_delivery_number: string
  self_coverage: string
  time: string
}

What i want to achieve is to make point_delivery_number an generic object property if its possible

It should look something like this:

export interface ChartDataResponseI<T> {
  consumption: string
  generation: string
  measure_name: string
  [T]: string
  self_coverage: string
  time: string
}

So i can pass the key like this:

ChartDataResponse<"point_delivery_number">

or

ChartDataResponse<"community_number">
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can use a simple intersection.

interface ChartDataResponseI {
  consumption: string
  generation: string
  measure_name: string
  self_coverage: string
  time: string  
}

type ChartDataWithProp<T> = ChartDataResponseI & T;

const chartData: ChartDataWithProp<{point_delivery_number: number, round: string}> = {
  consumption: 'string',
  generation: 'string',
  measure_name: 'string',
  self_coverage: 'string',
  time: 'string',  
  point_delivery_number: 2,
  round: 'yes'
};

TSPlayground

about 4 years ago · Juan Pablo Isaza Relatório

0

Do you really need to have it generic? If you only have those two keys that can differ, consider something like this:

interface ChartDataResponse {
  consumption: string
  generation: string
  measure_name: string
  self_coverage: string
  time: string
}

export interface ChartDataResponsePointDelivery extends ChartDataResponse {
    point_delivery_number: number
}

export interface ChartDataResponseCommunity extends ChartDataResponse {
    community_number: number
}

Now you have a base ChartDataResponse interface, which is extended for those two different cases. It is also easy to extend it further if needed. Both ChartDataResponsePointDelivery and ChartDataResponseCommunity have all the keys of ChartDataResponse plus the additional key.

Also note that the base interface is not exported at all, so you can't accidentally use the base interface with neither of the keys.

Often times when you are in a situation where you need to accept "any object", there is a deeper issue with how you are approaching the situation. TypeScript's whole point is to be there to enforce and to let you know what kind of objects you are dealing with and what keys they have.

about 4 years ago · Juan Pablo Isaza Relatório

0

use inheritance

interface ChartDataResponseI {
  consumption: string
  generation: string
  measure_name: string
  //point_delivery_number: string <- if its not default remove it
  self_coverage: string
  time: string
}

interface another extends ChartDataResponseI {
  point_delivery_number: string
  //or
  anything: string
}
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