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

90
Views
reference to partial object in typescript?

I have an object and it's typed

interface formDataType {
  tesla: {
    made: string
  },
  ford: {
    made: string
  }
}
const formData: formDataType = {
    "tesla": {
      "made":"2021"
    },
    "ford": {
      "made": "2022"
    }
  };

then I've a function where its argument is one of [string]:{made: string}, how can I use back formDataType? is there something like objectOf formDataType?

demo https://codesandbox.io/s/react-typescript-forked-cwv9p

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can create a type to use in your FormDataType, then reuse that type in your function:

TS Playground

type VehicleDetails = { made: string };

type FormDataType = {
  ford: VehicleDetails;
  tesla: VehicleDetails;
};

// The following is the same type, written using the Record utility:
// type FormDataType = Record<'ford' | 'tesla', VehicleDetails>;

const formData: FormDataType = {
  tesla: {made: '2021'},
  ford: {made: '2022'},
};

function doSomethingWithVehicleDetails (details: VehicleDetails) {
  // console.log(details);
}


Update in response to your comment:

You can also derive a union of all the types of the values of the keys in your FormData type (and if they're all the same type, the union will effectively be that one type):

TS Playground

type FormDataType = {
  ford: { made: string };
  tesla: { made: string };
};

type VehicleDetails = FormDataType[keyof FormDataType];

function doSomethingWithVehicleDetails (details: VehicleDetails) {
  // console.log(details);
}

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!