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

109
Views
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 answers
Answer question

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 Report

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 Report

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