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

97
Views
referencia a objeto parcial en mecanografiado?

Tengo un objeto y está escrito.

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

entonces tengo una función donde su argumento es uno de [string]:{made: string} , ¿cómo puedo usar formDataType? ¿Hay algo como objectOf formDataType ?

demostración https://codesandbox.io/s/react-typescript-forked-cwv9p

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

0

Puede crear un tipo para usar en su FormDataType y luego reutilizar ese tipo en su función:

Parque infantil TS

 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); }

Actualización en respuesta a su comentario :

También puede derivar una unión de todos los tipos de los valores de las claves en su tipo FormData (y si todos son del mismo tipo, la unión será efectivamente ese tipo):

Parque infantil TS

 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!